当前位置: 首页> 财经> 股票 > 网页设计专业设计课程_深圳找工作58同城最新招聘_进入百度搜索首页_重庆seo杨洋

网页设计专业设计课程_深圳找工作58同城最新招聘_进入百度搜索首页_重庆seo杨洋

时间:2025/7/13 10:33:36来源:https://blog.csdn.net/qq_21419015/article/details/143377644 浏览次数:0次
网页设计专业设计课程_深圳找工作58同城最新招聘_进入百度搜索首页_重庆seo杨洋

文章目录

  • 1、案例效果
  • 2、弹窗控件使用
    • 1.引入用户控件
    • 2、按钮命令实现
  • 3、总结
  • 4、源代码获取


1、案例效果

在这里插入图片描述

2、弹窗控件使用

1.引入用户控件

打开 Wpf_Examples 项目,在引用中添加用户控件库,在 MainWindow.xaml 界面引用控件库,代码如下(示例):

<Window x:Class="Wpf_Examples.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:converter="clr-namespace:Wpf_Examples.Converters"xmlns:local="clr-namespace:Wpf_Examples"xmlns:cc="clr-namespace:CustomControlLib;assembly=CustomControlLib"xmlns:uc="clr-namespace:UserControlLib;assembly=UserControlLib"DataContext="{Binding Source={StaticResource Locator},Path=Main}"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen"><Grid><WrapPanel><Button Width="120" Height="40" FontSize="18" Content="警告弹窗" Command="{Binding ButtonClickCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=Content}" Margin="8"/><Button Width="120" Height="40" FontSize="18" Content="错误弹窗" Command="{Binding ButtonClickCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=Content}" Margin="8"/><Button Width="120" Height="40" FontSize="18" Content="提示弹窗" Command="{Binding ButtonClickCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=Content}" Margin="8"/></WrapPanel></Grid>
</Window>

这里我们引用三种状态的弹窗来做实验,用 MessageBox 分别显示每种弹窗点击按钮的结果。

2、按钮命令实现

MainViewModel.cs 中按钮命令代码实现如下:

   public class MainViewModel : ObservableObject{public RelayCommand<string> ButtonClickCmd { get; set; }public MainViewModel(){ButtonClickCmd = new RelayCommand<string>(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框架的搭建WPF+MVVM案例实战(三)- 动态数字卡片效果实现

3、总结

以上,我们就已经实现了一个自定义弹窗控件的使用,感谢大家的关注,如果有想要其他WPF效果实现的小伙伴可以留言说明需求,本人看到后会根据需求推出对应功能实现的文章。

4、源代码获取

CSDN 下载链接:封装一个自定义消息弹窗控件

关键字:网页设计专业设计课程_深圳找工作58同城最新招聘_进入百度搜索首页_重庆seo杨洋

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: