廊坊网站制作官网,跨平台app开发框架,江苏省中医院网站建设,传奇手游排行榜2021前十名WPF不仅支持传统的Windows Forms编程的用户界面和用户体验设计#xff0c;同时还推出了以模板为核心的新一代设计理念。在WPF中#xff0c;通过引入模板#xff0c;将数据和算法的“内容”和“形式”进行解耦。模板主要分为两大类#xff1a;数据模板【Data Template】和控…WPF不仅支持传统的Windows Forms编程的用户界面和用户体验设计同时还推出了以模板为核心的新一代设计理念。在WPF中通过引入模板将数据和算法的“内容”和“形式”进行解耦。模板主要分为两大类数据模板【Data Template】和控件模板【Control Template】。 基本上ControlTemplate描述如何显示控件而DataTemplate描述如何显示数据。 控件模板 Control Template 控件模板让我们可以定义控件的外观改变控件的展现形式通过Control Template实现。 1. 编辑默认模板 选中控件--右键--编辑模板--编辑副本打开创建Style资源对话框如下所示 创建Style资源输入资源名称定义位置默认为此文档【Window】然后点击【确定】创建资源。如下所示 创建控件元素的默认资源如下所示
Window x:ClassWpfApp2.TwoWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp2mc:IgnorabledTitleTwoWindow Height350 Width800Window.ResourcesStyle 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:KeyOneButtonStyle 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}Border 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}//BorderControlTemplate.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/Window.ResourcesGridButton x:Nameone ContentHello wpf Margin5 Width100 Height30 Style{DynamicResource OneButtonStyle}/Button/Grid
/Window
编辑默认模板也可以通过【文档大纲】右键--编辑模板--编辑副本然后打开创建资源对话框进行操作如下所示 2. 修改默认样式 通过默认创建的控件模板Style可以修改和重定义控件的显示内容如设置按钮显示圆角和鼠标放上去为红色。 要实现以上功能只需要修改两个地方即可如下所示 3. 自定义控件模板 通过自定义模板同样能达到修改控件样式的效果。
控件模板也是资源的一种每一个控件模板都有一个唯一的key在控件上通过Template进行绑定如下所示
Window x:ClassWpfApp2.ThreeWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp2mc:IgnorabledTitle自定义控件模板示例 Height150 Width300Window.ResourcesControlTemplate x:KeyoneStyle TargetTypeButtonBorder BackgroundLightBlue CornerRadius5 x:NameborderStackPanel OrientationHorizontal HorizontalAlignment{TemplateBinding HorizontalAlignment}TextBlock VerticalAlignment{TemplateBinding VerticalAlignment}》》/TextBlockContentPresenter HorizontalAlignment{TemplateBinding HorizontalAlignment} VerticalAlignment{TemplateBinding VerticalAlignment}/ContentPresenter/StackPanel/BorderControlTemplate.TriggersTrigger PropertyIsMouseOver ValuetrueSetter PropertyBackground TargetNameborder ValueRed/Setter PropertyBorderBrush TargetNameborder ValueBlue//Trigger/ControlTemplate.Triggers/ControlTemplate/Window.ResourcesGridButton x:Nameone ContentHello wpf Margin5 Width100 Height30 VerticalAlignmentCenter HorizontalAlignmentCenter Template{StaticResource oneStyle}/Button/Grid
/Window 自定义控件模板示例如下 数据模板 DataTemplate 控件模板决定了数据的展示形式和用户体检在软件UI设计中非常重要。同样数据的展示形式越来越多样化正所谓横看成岭侧成峰远近高低各不同。同样的数据内容在DataGrid中的展示是文本的列表形式在ComboBox中是下拉框的形式。给数据披上外衣将数据和形式解耦是一种新的发展趋势。 1. DataGrid 1. 数据模板 DataGrid是可以自定义网格数据显示的控件通过自定义显示的列模板可以实现各式各样的展现方式。列定义如下 DataGrid的列定义通过Binding{Binding Name}的方式绑定属性通过ElementStyle{StaticResource one_center}的方式绑定样式。 DataGrid预制了几种列展示数据的方式如DataGridTextColumn【文本】DataGridCheckBoxColumn【复选框】DataGridComboBoxColumn【下拉框】DataGridHyperlinkColumn【链接】等如果使用数据模板则采用DataGridTemplateColumn进行定义。
UI示例如下所示
Window x:ClassWpfApp2.A1Windowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp2mc:IgnorabledTitle数据模板示例 Height450 Width650Window.ResourcesStyle x:Keyone_center TargetTypeTextBlockSetter PropertyVerticalAlignment ValueCenter/SetterSetter PropertyHorizontalAlignment ValueCenter/Setter/StyleStyle x:Keyone_header TargetTypeDataGridColumnHeaderSetter PropertyVerticalAlignment ValueCenter/SetterSetter PropertyHorizontalAlignment ValueCenter/SetterSetter PropertyHorizontalContentAlignment ValueCenter/SetterSetter PropertyBorderThickness Value0/Setter/Style/Window.ResourcesGridDataGrid x:Nameone Margin10 AutoGenerateColumnsFalse CanUserAddRowsFalse CanUserSortColumnsFalse BorderThickness0 DataGrid.ColumnsDataGridTextColumn Header姓名 Binding{Binding Name} Width* ElementStyle{StaticResource one_center} HeaderStyle{StaticResource one_header} /DataGridTextColumn Header年龄 Binding{Binding Age} Width* ElementStyle{StaticResource one_center} HeaderStyle{StaticResource one_header}/DataGridTextColumn Header性别 Binding{Binding Sex} Width* ElementStyle{StaticResource one_center} HeaderStyle{StaticResource one_header}/DataGridTextColumn Header班级 Binding{Binding Classes} Width* ElementStyle{StaticResource one_center} HeaderStyle{StaticResource one_header}/DataGridTemplateColumn Header操作 Width* HeaderStyle{StaticResource one_header}DataGridTemplateColumn.CellTemplateDataTemplateStackPanel OrientationHorizontal VerticalAlignmentCenter HorizontalAlignmentCenterButton x:Nameedit Content编辑 Width60 Margin3 Height25/ButtonButton x:Namedelete Content删除 Width60 Margin3 Height25/Button/StackPanel/DataTemplate/DataGridTemplateColumn.CellTemplate/DataGridTemplateColumn/DataGrid.Columns/DataGrid/Grid
/Window 2.后台数据绑定 后台数据绑定通过ItemsSource进行赋值绑定的数据的属性名要和DataGrid的列绑定数据的名称保持一致如下所示
namespace WpfApp2
{/// summary/// A1Window.xaml 的交互逻辑/// /summarypublic partial class A1Window : Window{public A1Window(){InitializeComponent();ListStudent lst new ListStudent();lst.Add(new Student() { Name 张三, Age 22, Sex 男, Classes 一班 });lst.Add(new Student() { Name 李四, Age 21, Sex 男, Classes 二班 });lst.Add(new Student() { Name 王五, Age 20, Sex 女, Classes 一班 });lst.Add(new Student() { Name 刘大, Age 19, Sex 男, Classes 三班 });lst.Add(new Student() { Name 麻子, Age 18, Sex 男, Classes 四班 });one.ItemsSource lst;}}public class Student{public string Name { get; set; }public int Age { get; set; }public string Sex { get; set; }public string Classes { get; set; }}
} DataGrid示例如下所示 2. ListBox和ComboBox 1. 数据模板 ListBoxComboBox均是包含可选择的项的列表只是ListBox不需要下拉显示ComboBox需要下拉显示。通过定义数据模板可以丰富数据的展示形式。
通过ItemTemplate{StaticResource item_template}的形式进行数据模板的绑定。如下所示
Window x:ClassWpfApp2.A2Windowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp2mc:IgnorabledTitle数据模板示例 Height450 Width800Window.ResourcesDataTemplate x:Keyitem_templateStackPanel OrientationHorizontal Margin5 ,0Border Width10 Height10 Background{Binding Code}/BorderTextBlock Text{Binding Code} Margin5,0 /TextBlock/StackPanel/DataTemplate/Window.ResourcesGridStackPanel Margin3 OrientationHorizontal HorizontalAlignmentCenter VerticalAlignmentCenterComboBox x:Nameone Height25 Width120 Margin5 ItemTemplate{StaticResource item_template}/ComboBoxListBox x:Nametwo Width120 Margin5 ItemTemplate{StaticResource item_template}/ListBox/StackPanel/Grid
/Window 2.后台数据绑定 与DataGrid一样后台通过ItemsSource进行数据的绑定。如下所示
namespace WpfApp2
{/// summary/// A2Window.xaml 的交互逻辑/// /summarypublic partial class A2Window : Window{public A2Window(){InitializeComponent();ListColor lst new ListColor();lst.Add(new Color() { Code #FE8C00 });lst.Add(new Color() { Code #1F7F50 });lst.Add(new Color() { Code #AA8C00 });lst.Add(new Color() { Code #FEAA00 });lst.Add(new Color() { Code #008CAA });lst.Add(new Color() { Code #FEBB00 });one.ItemsSource lst;two.ItemsSource lst;}}public class Color{public string Code { get; set; }}
}
示例截图如下所示 3. ItemsControl 1. 数据模板 ItemsControl主要用于展示集合数据的项也是列表控件的一种。ItemsControl 需要设置两个内容 ItemsControl.ItemsPanel做为数据展示的容器。 ItemsControl.ItemTemplate用于单个数据的展示形式。
具体如下所示
Window x:ClassWpfApp2.A3Windowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp2mc:IgnorabledTitleA3Window Height450 Width800GridItemsControl x:NameoneItemsControl.ItemsPanelItemsPanelTemplateWrapPanel/WrapPanel/ItemsPanelTemplate/ItemsControl.ItemsPanelItemsControl.ItemTemplateDataTemplateButton Width50 Height50 Margin5 Content{Binding Code}/Button/DataTemplate/ItemsControl.ItemTemplate/ItemsControl/Grid
/Window 2.后台数据绑定 与DataGrid一样后台通过ItemsSource进行数据的绑定。如下所示
namespace WpfApp2
{/// summary/// A3Window.xaml 的交互逻辑/// /summarypublic partial class A3Window : Window{public A3Window(){InitializeComponent();ListTest lst new ListTest();lst.Add(new Test() { Code 1 });lst.Add(new Test() { Code 2 });lst.Add(new Test() { Code 3 });lst.Add(new Test() { Code 4 });lst.Add(new Test() { Code 5 });lst.Add(new Test() { Code 6 });one.ItemsSource lst;}}public class Test{public string Code { get; set; }}
}
示例截图 控件模板和数据模板的应用场景还有很多本文旨在抛砖引玉一起学习共同进步。 文章转载自: http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.nypgb.cn.gov.cn.nypgb.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.hphrz.cn.gov.cn.hphrz.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.tjkth.cn.gov.cn.tjkth.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.hphfy.cn.gov.cn.hphfy.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.aishuxue.com.cn.gov.cn.aishuxue.com.cn http://www.morning.klrpm.cn.gov.cn.klrpm.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.bqppr.cn.gov.cn.bqppr.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn