当前位置: 首页 > news >正文

WPF自定义Dialog模板,内容用不同的Page填充

因为审美的不同,就总有些奇奇怪怪的需求,使用框架自带的对话框已经无法满足了,这里记录一下我这边初步设计的对话框。别问为啥要用模板嵌套Page来做对话框,问就是不想写太多的窗体。。。。

模板窗体(XAML)

<Window x:Class="换成自己的程序集.DialogModel"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:SCADA"mc:Ignorable="d"WindowStyle="None"WindowStartupLocation="CenterScreen"AllowsTransparency="True"Background="#060931"Foreground="White"Title="DialogModel" Height="450" Width="600"><Window.Resources><Style TargetType="DockPanel" x:Key="DockPanel"><Setter Property="Background" ><Setter.Value><ImageBrush ImageSource="/Images/top.png"/></Setter.Value></Setter></Style><Style TargetType="StackPanel" x:Key="StackPanel"><Setter Property="Background" ><Setter.Value><ImageBrush ImageSource="/Images/tb_1.png"/></Setter.Value></Setter></Style><Style TargetType="Button" x:Key="Button"><Setter Property="Background" ><Setter.Value><ImageBrush ImageSource="/Images/btn1.png"/></Setter.Value></Setter><Setter Property="BorderThickness" Value="0"/><Setter Property="BorderBrush" Value="Transparent"/><Setter Property="Foreground" Value="White"/></Style></Window.Resources><Grid x:Name="MyDialog"><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition/><RowDefinition Height="auto"/></Grid.RowDefinitions><!--头部--><DockPanel x:Name="TOP" Style="{StaticResource DockPanel}" VerticalAlignment="Center" Height="60" Margin="0"><TextBlock Text="{Binding DialogTitle}" Foreground="White" FontSize="20" VerticalAlignment="Center" Margin="20 0 0 0"/><WrapPanel   VerticalAlignment="Center" HorizontalAlignment="Right"><Button Height="20" Margin="5 0 5 0" Width="20" Padding="0"  Background="Transparent" BorderBrush="Transparent" Foreground="White" x:Name="btnMin" Content="—" /><Button Height="20" Margin="5 0 5 0" Width="20" Padding="0" Background="Transparent" BorderBrush="Transparent" Foreground="White" x:Name="btnMax" Content="❐" /><Button Height="20" Margin="5 0 5 0" Width="20" Padding="0" Background="Transparent" BorderBrush="Transparent" Foreground="White" x:Name="btnClose" Content="╳"/></WrapPanel></DockPanel><!--内容--><Frame x:Name="MainFrame" Content="{Binding CurrentPage}" Grid.Row="1" NavigationUIVisibility="Hidden" BorderThickness="0"/><StackPanel Grid.Row="2" Style="{StaticResource StackPanel}" Visibility="{Binding ISVisible}"><WrapPanel  HorizontalAlignment="Right" ><Button  Style="{StaticResource Button}" Margin=" 0 10 20 10" Click="Confirm" Height="40" Width="120" Content="确定" /><Button  Style="{StaticResource Button}" Margin=" 0 10 10 10" Height="40" Width="120" Click="Cancel"   Content="取消" /></WrapPanel></StackPanel></Grid>
</Window>

模板窗体(cs)

/// <summary>
/// DialogModel.xaml 的交互逻辑
/// </summary>
public partial class DialogModel : Window, INotifyPropertyChanged
{public event PropertyChangedEventHandler PropertyChanged;/// <summary>/// 消息通知方法/// </summary>/// <param name="propertyname"></param>public void OnPropertyChanged([CallerMemberName] string propertyname = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));}public DialogModel(Page page, string title,string Visible= "Visible"){InitializeComponent();DataContext = this;ISVisible = Visible;CurrentPage = page;DialogTitle = title;btnMin.Click += (s, e) => { this.WindowState = WindowState.Minimized; };btnMax.Click += (s, e) =>{if (this.WindowState == WindowState.Maximized)this.WindowState = WindowState.Normal;else{this.ResizeMode = ResizeMode.NoResize;this.WindowState = WindowState.Maximized;}};btnClose.Click += (s, e) => { this.Close(); };TOP.MouseMove += (s, e) =>{if (e.LeftButton == MouseButtonState.Pressed){this.DragMove();}};}#region 字段属性private string title;/// <summary>/// 对话框标题/// </summary>public string DialogTitle{get { return title; }set{title = value; OnPropertyChanged();}}private Page currentpage;public Page CurrentPage{get { return currentpage; }set{currentpage = value; OnPropertyChanged();}}private string visible;/// <summary>/// 是否显示确认取消按钮组,Visible-显示,Collapsed隐藏/// </summary>public string ISVisible{get { return visible; }set{visible = value; OnPropertyChanged();}}#endregion/// <summary>/// 确认方法,此处未返回值是因为主要内容都在Page里面。业务逻辑都在Page中。你也可以放置在此处,具体如何传值得靠你自己/// </summary>private void Confirm(object sender, RoutedEventArgs e){this.DialogResult = true;this.Close();}/// <summary>/// 取消方法/// </summary>private void Cancel(object sender, RoutedEventArgs e){this.Close();}
}

Page(XAML,引用了materialDesign库,没有的话用你自己定义的page就好)

<Page x:Class="*****.Pages.User"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:SCADA.Pages"xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"mc:Ignorable="d" Foreground="White"d:DesignHeight="450" d:DesignWidth="400"Title="User"><Page.Resources><Style TargetType="Grid" x:Key="Grid"><Setter Property="Background"><Setter.Value><ImageBrush ImageSource="/Images/tb_1.png"/></Setter.Value></Setter></Style></Page.Resources><Grid Style="{StaticResource Grid}"><StackPanel Margin="16" Width="300" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBox Margin="10"  Text="{Binding UserName}"materialDesign:HintAssist.Hint="UserName"/><TextBox Margin="10"  Text="{Binding Account}"materialDesign:HintAssist.Hint="Account"/><TextBox Margin="10"  Text="{Binding PassWord}"materialDesign:HintAssist.Hint="PassWord"/><ComboBox x:Name="CurrentRole" SelectionChanged="RoleChange"  Margin="10"  materialDesign:ComboBoxAssist.MaxLength="2" DisplayMemberPath="Name" SelectedValuePath="Id" ItemsSource="{Binding Roles}" materialDesign:HintAssist.Hint="请选择用户角色" materialDesign:HintAssist.HintOpacity=".26" IsEditable="True"></ComboBox><CheckBox Margin="10" Content="是否启用" IsChecked="{Binding IsActive}" /><StackPanel HorizontalAlignment="Right" Orientation="Horizontal"><Button Margin="0,8,8,0" Click="Confirm" Content="确认"></Button><Button Margin="0,8,8,0"  Click="Cancel" Content="取消"></Button></StackPanel></StackPanel></Grid>
</Page>

Page(.cs)

/// <summary>
/// User.xaml 的交互逻辑
/// </summary>
public partial class User : Page, INotifyPropertyChanged
{#region 通知public event PropertyChangedEventHandler PropertyChanged;/// <summary>/// 消息通知方法/// </summary>/// <param name="propertyname"></param>public void OnPropertyChanged([CallerMemberName] string propertyname = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));}#endregion
//这里的数据库用的EF,这里为啥这样写参考我前面的动态配置EF连接字符串的文章,此处可删string SQLConnection = EFConnection.GetConnection(".", "sa", "123456", "XYDB", false);public User(){InitializeComponent();DataContext = this;//必要//下面可删除//Roles = new List<SelectRole>();//Notice.MainUI = Application.Current.MainWindow;//string config = SysConfig.GetConfig(SysConfig.PathBase, "SysSet");//if (config != null)//{//    var mo = JsonConvert.DeserializeObject<SystemSetModel>(config);//    SQLConnection = EFConnection.GetConnection(mo.DbIP, mo.DbAccount, mo.DbPassWord, mo.DbName, false);//}在此初始化所有角色//using (var db = new DBEntities(SQLConnection))//{//    var role = db.SysRoles.Where(m => m.IsActive).ToList();//    if (role != null && role.Count > 0)//    {//        foreach (var item in role)//        {//            SelectRole role1 = new SelectRole();//            role1.Id = item.Id;//            role1.Name = item.RoleName;//            Roles.Add(role1);//        }//    }//}}#region 属性字段private Guid id;/// <summary>/// Id/// </summary>public Guid Id{get { return id; }set{id = value;OnPropertyChanged();//参数可以不写,}}private string username;/// <summary>///用户名/// </summary>public string UserName{get { return username; }set{username = value; OnPropertyChanged();}}private string account;/// <summary>///账号/// </summary>public string Account{get { return account; }set{account = value; OnPropertyChanged();}}private string password;/// <summary>///密码/// </summary>public string PassWord{get { return password; }set{password = value; OnPropertyChanged();}}private bool isactive;/// <summary>/// 状态/// </summary>public bool IsActive{get { return isactive; }set{isactive = value;OnPropertyChanged();}}private bool isadd = true;/// <summary>/// 是否添加用户,用于辨别确认方法是添加还是编辑/// </summary>public bool IsAdd{get { return isadd; }set{isadd = value;OnPropertyChanged();}}private Guid roleid;/// <summary>/// 选定的角色Id/// </summary>public Guid RoleId{get { return roleid; }set{roleid = value;OnPropertyChanged();//参数可以不写,}}private List<SelectRole> roles;/// <summary>/// 可选的角色列表/// </summary>public List<SelectRole> Roles{get { return roles; }set{roles = value;OnPropertyChanged();}}#endregion/// <summary>/// 添加/编辑/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Confirm(object sender, RoutedEventArgs e){try{//下面的逻辑替换成自己的//using (var db = new DBEntities(SQLConnection))//{//    //添加用户//    if (IsAdd)//    {//        var Current = db.SysUsers.FirstOrDefault(m => m.Account == Account);//        if (Current == null)//        {//            SysUser user = new SysUser();//            user.Id = Guid.NewGuid();//            user.Name = UserName;//            user.Account = Account;//            user.PassWord = PassWord;//            user.IsActive = IsActive;//            user.CreateTime = DateTime.Now;//            user.LoginNum = 0;//            user.LoginTime = DateTime.Now;//            User_Role user_Role = new User_Role();//            user_Role.Id = Guid.NewGuid();//            user_Role.UserId = user.Id;//            user_Role.RoleId = RoleId;//            user.User_Role.Add(user_Role);//            db.SysUsers.Add(user);//            db.SaveChanges();//            Notice.SuccessNotification_Dark("添加成功", 20, 5);//            // 获取当前页面所在的窗体//            Window window = Window.GetWindow(this);//            // 检查是否找到窗体并关闭它//            if (window != null)//            {//                window.Close();//            }//        }//        else//        {//            Notice.WarningNotification_Dark($"当前用户账号已存在!", 20, 5);//        }//    }//编辑用户//    else//    {//        var Current = db.SysUsers.FirstOrDefault(m => m.Id != Id && m.Account == Account);//        if (Current == null)//        {//            var User = db.SysUsers.FirstOrDefault(m => m.Id == Id);//            if (User != null)//            {//                db.Entry(User).State = EntityState.Detached;//取消跟踪免得修改主键冲突//                User.Name = UserName;//                User.Account = Account;//                User.PassWord = PassWord;//                User.IsActive = IsActive;//                var role = User.User_Role.FirstOrDefault();//                if (role != null)//有配置角色//                {//                    role.RoleId = RoleId;//                    db.User_Role.Add(role);//                }//                else//无配置角色//                {//                    User_Role userRole = new User_Role();//                    userRole.Id = Guid.NewGuid();//                    userRole.UserId = User.Id;//                    userRole.RoleId = RoleId;//                    db.User_Role.Add(userRole);//                }//                db.Entry(User).State = EntityState.Modified;//                db.SaveChanges();//                Notice.SuccessNotification_Dark("编辑成功", 20, 5);//                // 获取当前页面所在的窗体//                Window window = Window.GetWindow(this);//                // 检查是否找到窗体并关闭它//                if (window != null)//                {//                    window.Close();//                }//            }//            else//            {//                Notice.FailtureNotification_Dark("未找到当前用户", 20, 5);//                return;//            }//        }//        else//        {//            Notice.WarningNotification_Dark($"当前账号已存在!", 20, 5);//        }//    }//}}catch (Exception ex){Notice.FailtureNotification_Dark($"失败:{ex.Message}", 20, 5);}}/// <summary>/// 取消/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Cancel(object sender, RoutedEventArgs e){// 获取当前页面所在的窗体Window window = Window.GetWindow(this);// 检查是否找到窗体并关闭它if (window != null){window.Close();}}private void RoleChange(object sender, SelectionChangedEventArgs e){RoleId=(Guid)CurrentRole.SelectedValue;}
}
public class SelectRole
{public Guid Id { get; set; }public string Name { set; get; }
}

使用方法(供参考):

添加用户:

User user = new User();
DialogModel dialog = new DialogModel(user, "添加用户", "Collapsed");
dialog.ShowDialog();

编辑用户(需要赋值):

 /// <summary>/// DataGird的自定义操作栏的编辑按钮/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void UserEdit(object sender, RoutedEventArgs e){UserInfoDto userInfo = new UserInfoDto();var co = (Control)sender;userInfo = (UserInfoDto)co.DataContext;//获取当前行数据User user = new User();//实例化Pageuser.IsAdd = false;//标识是编辑user.Id = userInfo.Id;//将当前行信息给到Page,绑定到控件user.UserName = userInfo.Name;user.Account = userInfo.Account;user.PassWord = userInfo.PassWord;user.IsActive = userInfo.IsActive;user.RoleId = userInfo.RoleId;for (int i = 0; i < user.Roles.Count; i++){if (user.Roles[i].Id == userInfo.RoleId){if (user.Roles.Count > 1){user.Roles.Remove(user.Roles[i]);user.Roles.Insert(0, user.Roles[i]);}}}DialogModel dialog = new DialogModel(user, "编辑用户", "Collapsed");dialog.ShowDialog();}

最终结果(用到的背景图片啥的我就不贴了,毕竟我觉得不太好看):

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • OJ题-合并K个已排序的链表
  • libmodbus:写一个modbusTCP服务
  • 【AI学习】AI绘画发展简史
  • Unreal像素流ubantu os部署细节
  • 使用Maven创建一个Java项目并在repository中使用
  • qwen2 VL 多模态图文模型;图像、视频使用案例
  • ElK 8 收集 Nginx 日志
  • windows server2012 配制nginx安装为服务的时候,直接跳要安装.net框架,用自动的安装,直接失败的解决。
  • 从入门到精通,带你探索适合新手的视频剪辑工具
  • STM32快速复习(十二)FLASH闪存的读写
  • 海外服务器哪个速度最快且性能稳定
  • 鸿萌数据恢复服务: 修复 Windows, Mac, 手机中 “SD 卡无法读取”错误
  • 【git系列】git中的那些迷惑的术语以及概念详解
  • Linux(ubuntu)(c语言程序)
  • 算法训练——day16快乐数
  • 【译】JS基础算法脚本:字符串结尾
  • axios 和 cookie 的那些事
  • gcc介绍及安装
  • Hibernate【inverse和cascade属性】知识要点
  • iOS编译提示和导航提示
  • Java 多线程编程之:notify 和 wait 用法
  • JAVA多线程机制解析-volatilesynchronized
  • Java方法详解
  • mac修复ab及siege安装
  • Node 版本管理
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • webpack4 一点通
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • 从零开始学习部署
  • 如何用vue打造一个移动端音乐播放器
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • 带你开发类似Pokemon Go的AR游戏
  • 扩展资源服务器解决oauth2 性能瓶颈
  • #android不同版本废弃api,新api。
  • #pragam once 和 #ifndef 预编译头
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (ros//EnvironmentVariables)ros环境变量
  • (void) (_x == _y)的作用
  • (二)测试工具
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (计算机网络)物理层
  • (实战篇)如何缓存数据
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • .bat批处理出现中文乱码的情况
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .net core 6 使用注解自动注入实例,无需构造注入 autowrite4net
  • .Net CoreRabbitMQ消息存储可靠机制
  • .net 发送邮件