3.1.3 materialDesign:DialogHost 使用介绍
MaterialDesign是一个为WPF应用程序提供现代化UI组件的库,其中的DialogHost
组件用于显示和管理对话框。DialogHost
允许你轻松地弹出各种对话框,如消息框、确认框、输入对话框等,并且可以自定义对话框的样式和行为.
1.IsOpen
属性来控制对话框的显示和隐藏。
2. Identifier属性,用来标记DialogHost.
3.DialogContent 用于显示对话框的内容;当然也可以单独写一个usercontrol。
一、简单使用
点击按钮时,直接
<Window x:Class="AhuiPLC_CurveRecord.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:AhuiPLC_CurveRecord"mc:Ignorable="d"xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" Title="Window1" Height="450" Width="800"><Grid><StackPanel Orientation="Vertical"><Button Content="Open Dialog" Click="OpenDialog_Click" HorizontalAlignment="Center" VerticalAlignment="Center"/><Button Content="close Dialog" Click="CloseDialog_Click" HorizontalAlignment="Center" VerticalAlignment="Center"Margin="0 10 0 0"/><materialDesign:DialogHost x:Name="MyDialogHost" Identifier="MyDialog" Height="40" Margin="0,100,0,0" ><Border ><TextBlock HorizontalAlignment="Center" Background="LightBlue">"这是diaglog Host中的一个区域"</TextBlock></Border><materialDesign:DialogHost.DialogContent><TextBlock Text="This is a dialog content." HorizontalAlignment="Center" VerticalAlignment="Center"/></materialDesign:DialogHost.DialogContent></materialDesign:DialogHost></StackPanel> </Grid>
</Window>
private void OpenDialog_Click(object sender, RoutedEventArgs e){MyDialogHost.IsOpen = true;}// 关闭对话框的方法private void CloseDialog_Click(object sender, RoutedEventArgs e){MyDialogHost.IsOpen = false;}public Window1(){InitializeComponent();this.DataContext = this;}
例2:使用UserControl作为对话框的内容
主要是直接把UserControls的对话框添加到: dialogContent的内部
<materialDesign:DialogHost.DialogContent>
<controls:MyDialogControl/>
</materialDesign:DialogHost.DialogContent>
对话框如下:
<UserControl x:Class="AhuiPLC_CurveRecord.Controls.MyDialogControl"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:AhuiPLC_CurveRecord.Controls"mc:Ignorable="d" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"> <StackPanel Margin="16" Width="300"><TextBlock Text="请输入信息:" Style="{StaticResource MaterialDesignSubtitle1TextBlock}"/><TextBox materialDesign:HintAssist.Hint="用户名" Margin="0,8,0,0"/><PasswordBox materialDesign:HintAssist.Hint="密码" Margin="0,8,0,16"/><StackPanel Orientation="Horizontal" HorizontalAlignment="Right"><Button Content="取消" Command="{x:Static wpf:DialogHost.CloseDialogCommand}" CommandParameter="False"Margin="0,0,8,0"/><Button Content="确定" Command="{x:Static wpf:DialogHost.CloseDialogCommand}"CommandParameter="True"/></StackPanel></StackPanel>
</UserControl>
<Window x:Class="AhuiPLC_CurveRecord.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:AhuiPLC_CurveRecord"mc:Ignorable="d"xmlns:controls="clr-namespace:AhuiPLC_CurveRecord.Controls"xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" Title="Window1" Height="450" Width="800"><Grid><StackPanel Orientation="Vertical"><Button Content="Open Dialog" Click="OpenDialog_Click" HorizontalAlignment="Center" VerticalAlignment="Center"/><Button Content="close Dialog" Click="CloseDialog_Click" HorizontalAlignment="Center" VerticalAlignment="Center"Margin="0 10 0 0"/><materialDesign:DialogHost x:Name="MyDialogHost" Identifier="MyDialog" Height="40" Margin="0,100,0,0" ><Border ><TextBlock HorizontalAlignment="Center" Background="LightBlue">"这是diaglog Host中的一个区域"</TextBlock></Border><materialDesign:DialogHost.DialogContent><controls:MyDialogControl/></materialDesign:DialogHost.DialogContent></materialDesign:DialogHost></StackPanel> </Grid>
</Window>
private void OpenDialog_Click(object sender, RoutedEventArgs e)
{MyDialogHost.IsOpen = true;
}
// 关闭对话框的方法
private void CloseDialog_Click(object sender, RoutedEventArgs e)
{MyDialogHost.IsOpen = false;
}