前言
本文是《C# wpf style实现PART可替换控件》的另一种实现,是一种更优的方法,可以完全不涉及cs代码,设计器也可以实时预览。
一、什么是PART可替换控件?
参考《C# wpf style实现PART可替换控件》,在此处不再赘述。
二、如何实现?
核心方法是:《C# wpf Style中使用参数的方法》
1.定义参数
在Style的资源字典中定义一个用于PART替换的参数如下:
<ControlTemplate x:Key="WindowStyle_PART_Title"/>
2.关联参数
在Style的template中根据需要将可替换控件定义为ContentControl
标签 ,关联上述参数。
<ContentControl Template="{DynamicResource WindowStyle_PART_Title}"></ContentControl>
3.使用参数
在Style使用处的Resources标签中定义同名参数,然后再ControlTemplate
中定义实际需要的控件。示例如下:
<Window Style="{DynamicResource WindowStyle_Normal_Gray}" >
<Window.Resources>
<ControlTemplate x:Key="WindowStyle_PART_Title">
<Button Content="文件(F)" />
</ControlTemplate>
</Window.Resources>
</Window>
三、完整代码
完整的包括示例的项目代码:https://download.csdn.net/download/u013113678/39708684。
注意:本文主要是阐明一种用法,理解用法后很容易实现下列示例的代码,链接中的完整代码仅仅是示例的作用,请根据需要下载。
四、使用示例
个性化标题栏
在Window的资源字典中(示例):
<!--定义参数-->
<ControlTemplate x:Key="WindowStyle_PART_Title"/>
<!--Style略-->
<Style x:Key="WindowStyle_Normal_Gray" TargetType="Window"/>
<!--Style的Template中可替换的部分,关联参数WindowStyle_PART_Title-->
<ContentControl Template="{DynamicResource WindowStyle_PART_Title}"></ContentControl>
菜单窗口代码:
<Window x:Class="WpfApp1.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:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="720" Width="1280"
Style="{DynamicResource WindowStyle_Normal_Gray}"
>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="WindowStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!--标题栏WindowStyle_PART_Title可替换部分-->
<ControlTemplate x:Key="WindowStyle_PART_Title">
<!--定义菜单-->
<Button HorizontalAlignment="Left" Width="120" Height="50" Content="文件(F)" Background="#666666" Foreground="White" FontSize="24" Cursor="Hand">
<Button.Template >
<ControlTemplate TargetType="Button" >
<Border Background="{TemplateBinding Background}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}"></TextBlock>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</ControlTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid Background="White">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#999999" FontSize="64" Text="Hello Word!"></TextBlock>
</Grid>
</Window>
效果预览:
标题窗口代码:
<Window x:Class="WpfApp1.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:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="720" Width="1280"
Style="{DynamicResource WindowStyle_Normal_Gray}"
>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="WindowStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!--标题栏WindowStyle_PART_Title可替换部分-->
<ControlTemplate x:Key="WindowStyle_PART_Title">
<!--定义标题-->
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Hello Word Window" FontWeight="Bold" Foreground="White" FontSize="24" />
</ControlTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid Background="White">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#999999" FontSize="64" Text="Hello Word!"></TextBlock>
</Grid>
</Window>
效果预览:
总结
以上就是今天要讲的内容了,这种方法比之前使用附加属性的方法更优,无论是理解还是使用都相对容易,尤其是可以在设计器中实时显示,以及对Style的依赖也相对较轻了。总得来说,作为Style的一种使用技巧,这种方法是很有用的。