佛山大良网站建设招聘,网站建设培训心得,京东商城网站设计,南昌比较好的网站设计一、获取默认的控件模板
WPF修改控件模板是修改外观最方便的方式#xff0c;但是会出现不知道原来的控件的模板长什么样#xff0c;或者如何在原有控件模板上修改的#xff0c;下面就分享了获取某控件默认控件模板的方法#xff08;以控件Button为例#xff09;#xff…一、获取默认的控件模板
WPF修改控件模板是修改外观最方便的方式但是会出现不知道原来的控件的模板长什么样或者如何在原有控件模板上修改的下面就分享了获取某控件默认控件模板的方法以控件Button为例
1、创建一个Button
2、在界面上选择Button右键-编辑模板-编辑副本 就可以在XAML中看到自动生成的原始的控件模板代码
3、可以在默认模板上修改其中的一些属性并运行查看修改效果
这样在生成的默认控件模板上修改需要修改的部分即可可以大大减少工作量也提高了容错率。默认情况下所有的模板和样式都放在主界面的XAML中代码量会很多、很乱我们可以使用单独的资源词典来存放这些模板和样式主界面只要根据Key调用即可。
二、资源字典的使用
1、选中项目右键-添加-新建项-资源词典(WPF)
生成的初始资源词典如下
ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:TemplateDemo
/ResourceDictionary
现在可以将模板和样式作为资源分流到各个资源词典中了。我们现在演示将Button的默认模板转移至该控件模板 ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:TemplateDemoResourceDictionary.MergedDictionariesResourceDictionaryStyle x:KeyFocusVisualSetter PropertyControl.TemplateSetter.ValueControlTemplateRectangle Margin2 SnapsToDevicePixelstrue Stroke{DynamicResource {x:Static SystemColors.ControlTextBrushKey}} StrokeThickness1 StrokeDashArray1 2//ControlTemplate/Setter.Value/Setter/StyleSolidColorBrush x:KeyButton.Static.Background Color#FFDDDDDD/SolidColorBrush x:KeyButton.Static.Border Color#FF707070/SolidColorBrush x:KeyButton.MouseOver.Background Color#FFBEE6FD/SolidColorBrush x:KeyButton.MouseOver.Border Color#FF3C7FB1/SolidColorBrush x:KeyButton.Pressed.Background Color#FFC4E5F6/SolidColorBrush x:KeyButton.Pressed.Border Color#FF2C628B/SolidColorBrush x:KeyButton.Disabled.Background Color#FFF4F4F4/SolidColorBrush x:KeyButton.Disabled.Border Color#FFADB2B5/SolidColorBrush x:KeyButton.Disabled.Foreground Color#FF838383/Style x:KeyButtonStyle1 TargetType{x:Type Button}Setter PropertyFocusVisualStyle Value{StaticResource FocusVisual}/Setter PropertyBackground Value{StaticResource Button.Static.Background}/Setter PropertyBorderBrush Value{StaticResource Button.Static.Border}/Setter PropertyForeground Value{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}/Setter PropertyBorderThickness Value1/Setter PropertyHorizontalContentAlignment ValueCenter/Setter PropertyVerticalContentAlignment ValueCenter/Setter PropertyPadding Value1/Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type Button}StackPanelBorder x:Nameborder BorderBrush{TemplateBinding BorderBrush} BorderThickness{TemplateBinding BorderThickness} Background{TemplateBinding Background} SnapsToDevicePixelstrueContentPresenter x:NamecontentPresenter FocusableFalse HorizontalAlignment{TemplateBinding HorizontalContentAlignment} Margin{TemplateBinding Padding} RecognizesAccessKeyTrue SnapsToDevicePixels{TemplateBinding SnapsToDevicePixels} VerticalAlignment{TemplateBinding VerticalContentAlignment}//Border/StackPanelControlTemplate.TriggersTrigger PropertyIsDefaulted ValuetrueSetter PropertyBorderBrush TargetNameborder Value{DynamicResource {x:Static SystemColors.HighlightBrushKey}}//TriggerTrigger PropertyIsMouseOver ValuetrueSetter PropertyBackground TargetNameborder Value{StaticResource Button.MouseOver.Background}/Setter PropertyBorderBrush TargetNameborder Value{StaticResource Button.MouseOver.Border}//TriggerTrigger PropertyIsPressed ValuetrueSetter PropertyBackground TargetNameborder Value{StaticResource Button.Pressed.Background}/Setter PropertyBorderBrush TargetNameborder Value{StaticResource Button.Pressed.Border}//TriggerTrigger PropertyIsEnabled ValuefalseSetter PropertyBackground TargetNameborder Value{StaticResource Button.Disabled.Background}/Setter PropertyBorderBrush TargetNameborder Value{StaticResource Button.Disabled.Border}/Setter PropertyTextElement.Foreground TargetNamecontentPresenter Value{StaticResource Button.Disabled.Foreground}//Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style/ResourceDictionary/ResourceDictionary.MergedDictionaries
/ResourceDictionary
2、要使用该资源字典还需要在App.Xaml中进行声明我的名称叫TemplateDictionary.xaml需要保证其命名空间一致 Application.ResourcesResourceDictionary SourceTemplateDictionary.xaml/ResourceDictionary/Application.Resources
3、在主XAML中使用StaticResource或DynamicResource进行静态或动态引用即可
Button x:Namebutton ContentButton HorizontalAlignmentLeft Margin309,286,0,0 VerticalAlignmentTop Width75 Style{StaticResource ButtonStyle1}/
以上就是关于获取默认空间模板和使用资源词典的一些简单的介绍结合起来使用可以搭建简洁方便的代码布局