h5case是什么网站,php网站开发 vip,做环保的网站有哪些,做lol直播网站在 WPF#xff08;Windows Presentation Foundation#xff09;中#xff0c;模板是一种强大的机制#xff0c;用于定义控件的外观。它允许你将控件的逻辑#xff08;功能#xff09;和外观#xff08;UI#xff09;分离开来。例如#xff0c;一个按钮控件#xff0c… 在 WPFWindows Presentation Foundation中模板是一种强大的机制用于定义控件的外观。它允许你将控件的逻辑功能和外观UI分离开来。例如一个按钮控件其默认外观是由微软定义的但通过模板你可以完全改变按钮的外观如改变它的形状、颜色、添加动画效果等。 模板主要有两种类型ControlTemplate用于定义控件的可视结构和 DataTemplate用于定义数据对象的可视化表示。
1. 控件模板ControlTemplate
1.1 在本文件中定义模板
下面XAML文件定义了两个模板分别是Button和label控件的在界面正文时就可以直接调用资源中的控件模板
Window x:ClassTemplate.Views.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:prismhttp://prismlibrary.com/prism:ViewModelLocator.AutoWireViewModelTrueTitle{Binding Title} Height350 Width525 Window.Resources!-- 定义按钮控件模板 --ControlTemplate x:KeyMyButtonTemplate TargetTypeButtonBorder BackgroundLightBlue BorderBrushDarkBlue BorderThickness2 CornerRadius5ContentPresenter HorizontalAlignmentCenter VerticalAlignmentCenter//Border/ControlTemplate!--定义Label控件模板--ControlTemplate x:KeyLabelTemplate TargetTypeLabelBorder BackgroundLightBlue BorderBrushDarkBlue BorderThickness2 CornerRadius5ContentPresenter HorizontalAlignmentCenter VerticalAlignmentCenter//Border/ControlTemplate/Window.ResourcesGridStackPanel!-- 使用控件模板 --Button Template{StaticResource MyButtonTemplate} ContentButton 1 Width100 Height50/Button Template{StaticResource MyButtonTemplate} ContentButton 2 Width100 Height50/Label Template{StaticResource LabelTemplate } Content123 Width100 Height40//StackPanel/Grid
/Window
1.2 分离资源文添加模板
也可以将资源文件脱离出来单独管理便于在多个界面中使用同一模板
先添加资源文件 编辑资源文件
ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml!-- 定义按钮控件模板 --ControlTemplate x:KeyMyButtonTemplate TargetTypeButtonBorder BackgroundLightBlue BorderBrushDarkBlue BorderThickness2 CornerRadius5ContentPresenter HorizontalAlignmentCenter VerticalAlignmentCenter//Border/ControlTemplate!--定义Label控件模板--ControlTemplate x:KeyLabelTemplate TargetTypeLabelBorder BackgroundLightBlue BorderBrushDarkBlue BorderThickness2 CornerRadius5ContentPresenter HorizontalAlignmentCenter VerticalAlignmentCenter//Border/ControlTemplate/ResourceDictionary
呈现效果跟方法一是一样的 2. 数据模板DataTemplate
2.1 定义数据模型
public class Staff
{public string Name { get; set; }public int Age { get; set; }
}
2.2 在资源文件中定义模板
ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml!-- 定义按钮控件模板 --ControlTemplate x:KeyMyButtonTemplate TargetTypeButtonBorder BackgroundLightBlue BorderBrushDarkBlue BorderThickness2 CornerRadius5ContentPresenter HorizontalAlignmentCenter VerticalAlignmentCenter//Border/ControlTemplate!--定义Label控件模板--ControlTemplate x:KeyLabelTemplate TargetTypeLabelBorder BackgroundLightBlue BorderBrushDarkBlue BorderThickness2 CornerRadius5ContentPresenter HorizontalAlignmentCenter VerticalAlignmentCenter//Border/ControlTemplate!--定义数据模板--DataTemplate x:KeyStaffDataTemplateStackPanelTextBlock TextName: FontWeightBold/TextBlock Text{Binding Name} FontWeightBold/TextBlock TextAge: FontWeightBold/TextBlock Text{Binding Age} ForegroundGray//StackPanel/DataTemplate/ResourceDictionary
2.3 初始化数据模型
这里用了Prism框架
public class MainWindowViewModel : BindableBase
{private string _title Prism Application;public string Title{get { return _title; }set { SetProperty(ref _title, value); }}private ObservableCollectionStaff _people;public ObservableCollectionStaff People{get { return _people; }set { SetProperty(ref _people, value); }}public MainWindowViewModel(){// 初始化数据People new ObservableCollectionStaff{new Staff { Name Alice, Age 25 },new Staff { Name Bob, Age 30 },new Staff { Name Charlie, Age 35 }};}
}
2.4 应用数据模板绑定数据
Window x:ClassTemplate.Views.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:prismhttp://prismlibrary.com/prism:ViewModelLocator.AutoWireViewModelTrueTitle{Binding Title} Height350 Width525 Window.Resources!-- 引用外部的资源字典 --ResourceDictionary SourceControlTemplates.xaml//Window.ResourcesGridStackPanel!-- 使用控件模板 --Button Template{StaticResource MyButtonTemplate} ContentButton 1 Width100 Height50/Button Template{StaticResource MyButtonTemplate} ContentButton 2 Width100 Height50/Label Template{StaticResource LabelTemplate } Content123 Width100 Height40/!--这样--ListBox ItemTemplate{StaticResource StaffDataTemplate} ItemsSource{Binding People}/ListBox!--或者这样--StackPanelListBox ItemsSource{Binding People} ItemTemplate{StaticResource StaffDataTemplate}//StackPanel/StackPanel/Grid
/Window运行效果 3. 基于模板的样式 StylewithTemplates
3.1 在资源字典中定义一个样式
ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml!--定义样式--Style x:KeyMyButtonStyle TargetTypeButtonSetter PropertyBackground ValueLightGreen/Setter PropertyForeground ValueWhite/Setter PropertyFontWeight ValueBold//Style
/ResourceDictionary
3.2 使用样式
Window x:ClassTemplate.Views.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:prismhttp://prismlibrary.com/prism:ViewModelLocator.AutoWireViewModelTrueTitle{Binding Title} Height350 Width525 Window.Resources!-- 引用外部的资源字典 --ResourceDictionary SourceControlTemplates.xaml //Window.ResourcesGridStackPanel!-- 使用样式 --Button Style{StaticResource MyButtonStyle} ContentButton 1 Width100 Height50/Button Style{StaticResource MyButtonStyle} ContentButton 2 Width100 Height50/Button Style{StaticResource MyButtonStyle} ContentButton 3 Width100 Height50/Button Style{StaticResource MyButtonStyle} ContentButton 4 Width100 Height50//StackPanel/Grid
/Window运行效果 4. WPF的模板资源
HandyControl这是一套比较流行的 WPF 控件库几乎重写了所有原生样式并且包含 80 余款自定义控件。在其开源项目中有大量的模板定义和使用案例对于想要快速构建美观的 WPF 应用程序的开发者来说非常有价值。你可以访问HandyControl 的 GitHub 页面获取相关资源。Panuon.WPF.UI该组件库能帮助开发者快速完成样式和控件的 UI 设计提供了大量的属性来方便地修改 WPF 中一些常用的效果而这些效果的实现往往涉及到模板的使用。你可以在Panuon.WPF.UI 的 GitHub 页面上找到相关的代码和示例。ModernUI提供了基于 ModernUI 1.0.4 的 win8 极简化界面设计有丰富的界面组件和样式支持插件化开发便于扩展和维护。你可以从ModernUI 的项目地址获取相关资源。