这篇文章给大家介绍C# 中怎么利用WPF实现一个遮罩对话框,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
App.xaml引入MD控件样式
<Application x:Class="DropDownMenu.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DropDownMenu" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml"/> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml"/> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources></Application>
MainWindow.xaml,整体布局,看上图加上下面的界面代码,添加界面左上角logo图标、左侧导航菜单等,下面即是全部代码。
<Window x:Class="ScreenOverlayMessage.MainWindow" 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:ScreenOverlayMessage" mc:Ignorable="d" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" MouseDown="Window_MouseDown" Title="MainWindow" Height="576" Width="1024" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" Background="#FF423A3A"> <Grid> <materialDesign:DialogHost BorderBrush="{DynamicResource MaterialDesignDivider}"> <materialDesign:DialogHost.DialogContent> <Grid Width="300" Height="150" HorizontalAlignment="Center"> <StackPanel Orientation="Horizontal" Margin="15"> <materialDesign:PackIcon Kind="Folder" Foreground="{StaticResource PrimaryHueMidBrush}" Width="50" Height="50"/> <TextBlock Foreground="Gray" Width="200" Margin="15 5" TextWrapping="Wrap"> 允许应用程序访问您计算机上的照片、媒体和文件? </TextBlock> </StackPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="15"> <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Style="{DynamicResource MaterialDesignFlatButton}" Margin="4" VerticalAlignment="Center"> 拒绝 </Button> <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Style="{DynamicResource MaterialDesignFlatButton}" Margin="4" VerticalAlignment="Center"> 允许 </Button> </StackPanel> </Grid> </materialDesign:DialogHost.DialogContent> <Grid> <StackPanel Width="200" HorizontalAlignment="Left" Background="#FF472076"> <Grid Height="150" Background="White"> <Image Source="https://img.dotnet9.com/logo.png"/> </Grid> <Button Style="{StaticResource MaterialDesignFlatButton}" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"> <StackPanel Orientation="Horizontal" Height="30"> <materialDesign:PackIcon Kind="PhotoAlbum" Width="20" Height="20" VerticalAlignment="Center"/> <TextBlock Text="照片" Margin="20 0" FontSize="15" VerticalAlignment="Center"/> </StackPanel> </Button> <Button Style="{StaticResource MaterialDesignFlatButton}" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"> <StackPanel Orientation="Horizontal" Height="30"> <materialDesign:PackIcon Kind="Music" Width="20" Height="20" VerticalAlignment="Center"/> <TextBlock Text="音乐" Margin="20 0" FontSize="15" VerticalAlignment="Center"/> </StackPanel> </Button> </StackPanel> </Grid> </materialDesign:DialogHost> </Grid></Window>
重点讲解:
materialDesign:DialogHost:设置遮罩对话框覆盖的地方,即弹出遮罩对话框后,背后有阴影的位置。
materialDesign:DialogHost.DialogContent:对话框内容,即弹出的对话框配置
后台有个拖动窗体代码:
private void Window_MouseDown(object sender, MouseButtonEventArgs e){ DragMove();}
关于C# 中怎么利用WPF实现一个遮罩对话框就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/dotnet9/blog/4442810