• 在WPF中使用Prism弹出自定义窗体样式的对话框


    概述

    我们在Prism中弹出一个对话框,默认是一个Windows默认样式的窗口,这样会同自己所开发的项目完全不搭调,譬如下面这样子

     那么如果为了配合软件主体的风格,可以做出类似这样效果

     其实原理也很简单,Prism也考虑到了这一点,所以特意设计一个供用户自定义的接口

    编写组件样式

    1、新建一个Window视图

     注意Window里的一些必要属性记得设置一下,比如SizeToContent ShowInTaskbar等等实现无边框还是使用常规WindowChrome做法

    1. <WindowChrome.WindowChrome>
    2. <WindowChrome
    3. CaptionHeight="60"
    4. CornerRadius="0"
    5. GlassFrameThickness="0" />
    6. </WindowChrome.WindowChrome>

    其中CaptionHeight表示可以拖拽的高度,这里一般建议与自己所设计的高度一致,关于WindowChrome相关用法和介绍就不过多赘述,具体可前往MSDN文档:WindowChrome 类

    2、根据自己需求或设计图重写该窗体样式,XAML太多就不一一介绍了,直接上代码

    1. <Window
    2. x:Class="使用Prism弹出自定义窗体样式的对话框.Components.DialogWindowView"
    3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    6. xmlns:local="clr-namespace:使用Prism弹出自定义窗体样式的对话框.Components"
    7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    8. Title="{Binding Title}"
    9. Width="800"
    10. Height="450"
    11. ResizeMode="NoResize"
    12. SizeToContent="WidthAndHeight"
    13. WindowStartupLocation="CenterScreen"
    14. WindowStyle="None"
    15. mc:Ignorable="d">
    16. <Window.Resources>
    17. <!-- 画刷 -->
    18. <SolidColorBrush x:Key="DialogBackgroundColor" Color="#040D14" />
    19. <SolidColorBrush x:Key="DialogBorderBrushColor" Color="#6C83A4" />
    20. <SolidColorBrush x:Key="DialogTitleColor" Color="#90CFFF" />
    21. <ImageBrush x:Key="DialogTitleImageBrush" ImageSource="/使用Prism弹出自定义窗体样式的对话框;component/Assets/Images/dialog_heder_bg.png" />
    22. <LinearGradientBrush x:Key="TitleTextGradientColor" StartPoint="0,0" EndPoint="0,1">
    23. <GradientStop Offset="1" Color="#A5BCF3" />
    24. <GradientStop Offset="0" Color="White" />
    25. </LinearGradientBrush>
    26. <!-- 字体 -->
    27. <FontFamily x:Key="TitleFontFamily">/使用Prism弹出自定义窗体样式的对话框;component/Assets/Fonts/#庞门正道标题体3.0</FontFamily>
    28. <!-- 对话框标题样式 -->
    29. <Style x:Key="SystemTitleStyle" TargetType="{x:Type TextBlock}">
    30. <Setter Property="VerticalAlignment" Value="Center" />
    31. <Setter Property="HorizontalAlignment" Value="Center" />
    32. <Setter Property="TextAlignment" Value="Center" />
    33. <Setter Property="FontSize" Value="15" />
    34. <Setter Property="FontFamily" Value="{StaticResource TitleFontFamily}" />
    35. <Setter Property="FontSize" Value="30" />
    36. <Setter Property="Foreground" Value="{StaticResource TitleTextGradientColor}" />
    37. </Style>
    38. <!-- 按钮样式 -->
    39. <Style x:Key="IconButtonBaseStyle" TargetType="Button">
    40. <Setter Property="Cursor" Value="Hand" />
    41. <Setter Property="Template">
    42. <Setter.Value>
    43. <ControlTemplate TargetType="Button">
    44. <TextBlock
    45. x:Name="icon"
    46. HorizontalAlignment="Center"
    47. VerticalAlignment="Center"
    48. FontSize="{TemplateBinding FontSize}"
    49. Foreground="{TemplateBinding Foreground}"
    50. Text="{TemplateBinding Content}" />
    51. <ControlTemplate.Triggers>
    52. <Trigger Property="IsMouseOver" Value="True">
    53. <Setter TargetName="icon" Property="Foreground" Value="#90CFFF" />
    54. </Trigger>
    55. </ControlTemplate.Triggers>
    56. </ControlTemplate>
    57. </Setter.Value>
    58. </Setter>
    59. </Style>
    60. </Window.Resources>
    61. <Window.Style>
    62. <Style TargetType="{x:Type Window}">
    63. <Setter Property="ShowInTaskbar" Value="False" />
    64. <Setter Property="Template">
    65. <Setter.Value>
    66. <ControlTemplate TargetType="{x:Type Window}">
    67. <Border
    68. Background="{StaticResource DialogBackgroundColor}"
    69. BorderBrush="{StaticResource DialogBorderBrushColor}"
    70. BorderThickness="2">
    71. <Grid>
    72. <Grid.RowDefinitions>
    73. <RowDefinition Height="60" />
    74. <RowDefinition />
    75. </Grid.RowDefinitions>
    76. <Border Background="{StaticResource DialogTitleImageBrush}">
    77. <Grid>
    78. <Grid.ColumnDefinitions>
    79. <ColumnDefinition />
    80. <ColumnDefinition />
    81. </Grid.ColumnDefinitions>
    82. <TextBlock
    83. Margin="10,0,0,0"
    84. HorizontalAlignment="Left"
    85. Foreground="{StaticResource DialogTitleColor}"
    86. Style="{StaticResource SystemTitleStyle}"
    87. Text="{TemplateBinding Title}" />
    88. <StackPanel
    89. Grid.Column="2"
    90. HorizontalAlignment="Right"
    91. VerticalAlignment="Center"
    92. Orientation="Horizontal"
    93. WindowChrome.IsHitTestVisibleInChrome="True">
    94. <Button
    95. Margin="0,0,20,0"
    96. Command="ApplicationCommands.Close"
    97. Content="x"
    98. Cursor="Hand"
    99. FontSize="30"
    100. FontWeight="Medium"
    101. Foreground="White"
    102. Style="{StaticResource IconButtonBaseStyle}" />
    103. </StackPanel>
    104. </Grid>
    105. </Border>
    106. <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" />
    107. </Grid>
    108. </Border>
    109. </ControlTemplate>
    110. </Setter.Value>
    111. </Setter>
    112. </Style>
    113. </Window.Style>
    114. <WindowChrome.WindowChrome>
    115. <WindowChrome
    116. CaptionHeight="60"
    117. CornerRadius="0"
    118. GlassFrameThickness="0" />
    119. </WindowChrome.WindowChrome>
    120. </Window>

    Tips:因为一般对话框分为标题栏和主体部分,所以我们可以使用Grid分为两行,第一行是标题,第二行是内容,使用ContentPresenter来承载要显示的用户控件

    使用组件

    1、将自定义的样式组件注入到IOC容器

    1. protected override void RegisterTypes(IContainerRegistry containerRegistry)
    2. {
    3. containerRegistry.Register(nameof(DialogWindowView));
    4. }

    2、使用DialogService弹出对话框

    1. private void CustomOpenDialog()
    2. {
    3. DialogParameters dialogParameters = new DialogParameters()
    4. {
    5. {"Title",$"忧郁的蛋 {DateTime.Now:yyyy-MM-dd}" }
    6. };
    7. dialogService.Show(nameof(TestDialogView), dialogParameters, result =>
    8. {
    9. }, nameof(DialogWindowView));
    10. }

    这段与传统打开弹框是差不多,唯一不同的就是使用到了一个可传入WindowName的重载函数,Tips:我们自定义的这个组件(DialogWindowView)是一定需要注入到IOC容器的,不然拿不到这个实例就会抛出异常,另外还有一个点就是该组件还需继承自IDialogWindow

    效果 

     整个程序代码已传到CSDN,感兴趣的朋友可以去看看,代码下载地址:在WPF中使用Prism弹出自定义窗体样式的对话框

  • 相关阅读:
    如何调用Metabase开放API
    基于SSM的班级事务管理系统
    Web安全系列——敏感信息泄露与加密机制
    深度学习之环境配置 jupyter notebook
    【图像压缩】基于matlab二叉树和优化截断(BTOT)遥感图像压缩【含Matlab源码 2043期】
    漏洞深度分析|Apache Fineract 远程代码执行漏洞
    【云原生之Docker实战】使用Docker部署Pydio Cells文件分享工具
    Java集合之List、Set
    计算ip是否在网络段(子网掩码)
    单元测试界的高富帅Pytest框架,手把手教学,从入门到精通
  • 原文地址:https://blog.csdn.net/lwf3115841/article/details/127937610