【转】WPF默认控件模板的获取和资源词典的使用
一、獲取默認(rèn)的控件模板
WPF修改控件模板是修改外觀最方便的方式,但是會(huì)出現(xiàn)不知道原來(lái)的控件的模板長(zhǎng)什么樣,或者如何在原有控件模板上修改的,下面就分享了獲取某控件默認(rèn)控件模板的方法(以控件Button為例):
1、創(chuàng)建一個(gè)Button
2、在界面上選擇Button,右鍵->編輯模板->編輯副本?,就可以在XAML中看到自動(dòng)生成的原始的控件模板代碼
3、可以在默認(rèn)模板上修改其中的一些屬性,并運(yùn)行查看修改效果
這樣在生成的默認(rèn)控件模板上,修改需要修改的部分即可,可以大大減少工作量,也提高了容錯(cuò)率。默認(rèn)情況下,所有的模板和樣式都放在主界面的XAML中,代碼量會(huì)很多、很亂,我們可以使用單獨(dú)的資源詞典來(lái)存放這些模板和樣式,主界面只要根據(jù)Key調(diào)用即可。
二、資源字典的使用
1、選中項(xiàng)目右鍵->添加->新建項(xiàng)->資源詞典(WPF)
生成的初始資源詞典如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:TemplateDemo"> </ResourceDictionary>現(xiàn)在可以將模板和樣式作為資源分流到各個(gè)資源詞典中了。我們現(xiàn)在演示將Button的默認(rèn)模板轉(zhuǎn)移至該控件模板
?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:TemplateDemo"><ResourceDictionary.MergedDictionaries><ResourceDictionary><Style x:Key="FocusVisual"><Setter Property="Control.Template"><Setter.Value><ControlTemplate><Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/></ControlTemplate></Setter.Value></Setter></Style><SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/><SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/><SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/><SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/><SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/><SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/><SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/><SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/><SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/><Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"><Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/><Setter Property="Background" Value="{StaticResource Button.Static.Background}"/><Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/><Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/><Setter Property="BorderThickness" Value="1"/><Setter Property="HorizontalContentAlignment" Value="Center"/><Setter Property="VerticalContentAlignment" Value="Center"/><Setter Property="Padding" Value="1"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><StackPanel><Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"><ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/></Border></StackPanel><ControlTemplate.Triggers><Trigger Property="IsDefaulted" Value="true"><Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/></Trigger><Trigger Property="IsMouseOver" Value="true"><Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/><Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/></Trigger><Trigger Property="IsPressed" Value="true"><Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/><Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/></Trigger><Trigger Property="IsEnabled" Value="false"><Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/><Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/><Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></ResourceDictionary></ResourceDictionary.MergedDictionaries> </ResourceDictionary>2、要使用該資源字典還需要在App.Xaml中進(jìn)行聲明,我的名稱叫TemplateDictionary.xaml,需要保證其命名空間一致
<Application.Resources><ResourceDictionary Source="TemplateDictionary.xaml"></ResourceDictionary></Application.Resources>3、在主XAML中使用StaticResource或DynamicResource進(jìn)行靜態(tài)或動(dòng)態(tài)引用即可
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="309,286,0,0" VerticalAlignment="Top" Width="75" Style="{StaticResource ButtonStyle1}"/>以上就是關(guān)于獲取默認(rèn)空間模板和使用資源詞典的一些簡(jiǎn)單的介紹,結(jié)合起來(lái)使用可以搭建簡(jiǎn)潔方便的代碼布局
總結(jié)
以上是生活随笔為你收集整理的【转】WPF默认控件模板的获取和资源词典的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 特斯拉裁员“屠龙刀”终于落下!马斯克:感
- 下一篇: 吉利拟收购魅族上热搜 网友泪目:终于成大