橙 建网站,WordPress 插件调试,汉语言专业简历制作说明,自己申请网站空间文章目录 1、案例效果2、弹窗控件使用1.引入用户控件2、按钮命令实现 3、总结4、源代码获取 1、案例效果 2、弹窗控件使用
1.引入用户控件
打开 Wpf_Examples 项目#xff0c;在引用中添加用户控件库#xff0c;在 MainWindow.xaml 界面引用控件库#xff0c;代码如下在引用中添加用户控件库在 MainWindow.xaml 界面引用控件库代码如下示例
Window x:ClassWpf_Examples.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:converterclr-namespace:Wpf_Examples.Convertersxmlns:localclr-namespace:Wpf_Examplesxmlns:ccclr-namespace:CustomControlLib;assemblyCustomControlLibxmlns:ucclr-namespace:UserControlLib;assemblyUserControlLibDataContext{Binding Source{StaticResource Locator},PathMain}mc:IgnorabledTitleMainWindow Height450 Width800 WindowStartupLocationCenterScreenGridWrapPanelButton Width120 Height40 FontSize18 Content警告弹窗 Command{Binding ButtonClickCmd} CommandParameter{Binding RelativeSource{RelativeSource ModeSelf},PathContent} Margin8/Button Width120 Height40 FontSize18 Content错误弹窗 Command{Binding ButtonClickCmd} CommandParameter{Binding RelativeSource{RelativeSource ModeSelf},PathContent} Margin8/Button Width120 Height40 FontSize18 Content提示弹窗 Command{Binding ButtonClickCmd} CommandParameter{Binding RelativeSource{RelativeSource ModeSelf},PathContent} Margin8//WrapPanel/Grid
/Window
这里我们引用三种状态的弹窗来做实验用 MessageBox 分别显示每种弹窗点击按钮的结果。
2、按钮命令实现
MainViewModel.cs 中按钮命令代码实现如下 public class MainViewModel : ObservableObject{public RelayCommandstring ButtonClickCmd { get; set; }public MainViewModel(){ButtonClickCmd new RelayCommandstring(FunMenu);}private void FunMenu(string obj){var mainWindowInstance App.Current.MainWindow; // 获取主窗口实例bool? result;//定义窗体点击的结果选项switch (obj){case 警告弹窗:var warningNotification new UserControlLib.SMessageBox(){Icon IconType.Warning,ButtonType ButtonType.OkCancel,Title 警告,Content 这是一条警告信息,};warningNotification.Owner mainWindowInstance; // 设置父窗口result warningNotification.ShowDialog();MessageBox.Show($点击窗体的结果是{result}, 提示, MessageBoxButton.OK);break;case 错误弹窗:var error new UserControlLib.SMessageBox(){Icon IconType.Error,ButtonType ButtonType.Ok,Title 错误,Content 这是一条错误信息,};error.Owner mainWindowInstance; // 设置父窗口result error.ShowDialog();MessageBox.Show($点击窗体的结果是{result}, 提示, MessageBoxButton.OK);break;case 提示弹窗:var info new UserControlLib.SMessageBox(){Icon IconType.Info,ButtonType ButtonType.Ok,Title 错误,Content 这是一条错误信息,};info.Owner mainWindowInstance; // 设置父窗口result info.ShowDialog();MessageBox.Show($点击窗体的结果是{result}, 提示, MessageBoxButton.OK);break;}}private void PopWindow(Window window){var mainWindowInstance App.Current.MainWindow; // 获取主窗口实例window.Owner mainWindowInstance;window.WindowStartupLocation WindowStartupLocation.CenterOwner;window.ShowDialog();}}Wpf_Examples 项目没有的同学可以看前面的章节。完成整个项目及MVVM框架的搭建WPFMVVM案例实战三- 动态数字卡片效果实现
3、总结
以上我们就已经实现了一个自定义弹窗控件的使用感谢大家的关注如果有想要其他WPF效果实现的小伙伴可以留言说明需求本人看到后会根据需求推出对应功能实现的文章。
4、源代码获取
CSDN 下载链接封装一个自定义消息弹窗控件