网站access数据怎么做,网站的标题符号,建站行业突破,枣庄哪里有做网站设计模板是描述控件外观#xff0c;WPF中每个控件都有一个默认的模板#xff0c;你可以通过定义模板来重写控件可视化外观和行为#xff0c;WPF中有两种常用的模板Control Template和Data TemplateControl Template控件模板定义了控件的可视化外观#xff0c;所有的UI控件都有自…模板是描述控件外观WPF中每个控件都有一个默认的模板你可以通过定义模板来重写控件可视化外观和行为WPF中有两种常用的模板Control Template和Data TemplateControl Template控件模板定义了控件的可视化外观所有的UI控件都有自己默认的可视化外观和行为例如Button按钮当我们鼠标移上去时会改变背景色我们自定义一个Button按钮Window x:ClassExample_09.MainWindowxmlnshttp://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:Example_09mc:Ignorabled TitleMainWindow Height450 Width800Window.ResourcesControlTemplate x:KeyButtonTemp TargetTypeButtonGridEllipse x:NamerectangleEllipse.FillSolidColorBrush ColorRed/SolidColorBrush/Ellipse.Fill/EllipseContentPresenter Content{TemplateBinding Content} VerticalAlignmentCenterHorizontalAlignmentCenter/ContentPresenter/GridControlTemplate.TriggersTrigger PropertyIsMouseOver ValueTrueSetter TargetNamerectangle PropertyFillSetter.ValueSolidColorBrush ColorGreen/SolidColorBrush/Setter.Value/Setter/Trigger/ControlTemplate.Triggers/ControlTemplate/Window.ResourcesStackPanelButton Width200 Height150 Content自定义控件模板 Template{StaticResource ButtonTemp}/ButtonButton Width200 Height150 Margin20 Content默认控件外观/Button/StackPanel
/WindowData Template数据模板定义了你的数据在控件中如何呈现以及格式化。通常用在列表控件中例如ComboBox, ListBox, 等默认ListBox绑定出来数据的结果我们通过重写数据模板显示内容更丰富一些Window x:ClassExample_09.ListBoxControlxmlnshttp://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:Example_09mc:IgnorabledTitleListBoxControl Height450 Width800Window.ResourcesDataTemplate x:KeypersonTemplateBorder Nameborder BorderBrushAqua BorderThickness1 Padding5 Margin5GridGrid.RowDefinitionsRowDefinition/RowDefinition/RowDefinition/RowDefinition//Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition Width200/ColumnDefinition Width100/ColumnDefinition Width100//Grid.ColumnDefinitionsTextBlock Grid.Row0 Grid.Column1 HorizontalAlignmentCenter Text姓名:/TextBlock Grid.Row0 Grid.Column2 HorizontalAlignmentCenter Text{Binding PathName} /TextBlock Grid.Row1 Grid.Column1 HorizontalAlignmentCenter Text性别:/TextBlock Grid.Row1 Grid.Column2 HorizontalAlignmentCenter Text{Binding PathSex}/TextBlock Grid.Row2 Grid.Column1 HorizontalAlignmentCenter Text年龄:/TextBlock Grid.Row2 Grid.Column2 HorizontalAlignmentCenter Text{Binding PathAge}/TextBlock Grid.Row3 Grid.Column1 HorizontalAlignmentCenter Text户籍:/TextBlock Grid.Row4 Grid.Column2 HorizontalAlignmentCenter Text{Binding PathResidence}/Image Grid.RowSpan4 Grid.Column0 HorizontalAlignmentCenter Source{Binding PathImage}/Image/Grid/Border/DataTemplate/Window.ResourcesGridListBox x:NamemyListBox ItemTemplate{StaticResource personTemplate}/ListBox/Grid
/Windowusing System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media.Imaging;
namespace Example_09
{public partial class ListBoxControl : Window{ObservableCollectionPerson Persons new ObservableCollectionPerson();public ListBoxControl(){InitializeComponent();Person person new Person();myListBox.ItemsSource person.GetPersonList();}}public class Person{public string Name { get; set; }public string Sex { get; set; }public int Age { get; set; }public string Residence{get; set;} Hong Kong;public BitmapImage Image { get; set; }public override string ToString(){return Name.ToString();}public ObservableCollectionPerson GetPersonList(){ObservableCollectionPerson Persons new ObservableCollectionPerson();Person person new Person();person.Name 刘德华;person.Sex 男;person.Age 60;person.Image new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() /Image/刘德华.jpg));Persons.Add(person);person new Person();person.Name 黎明;person.Sex 男;person.Age 55;person.Image new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() /Image/黎明.jpg));Persons.Add(person);person new Person();person.Name 郭富城;person.Sex 男;person.Age 56;person.Image new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() /Image/郭富城.jpg));Persons.Add(person);person new Person();person.Name 张学友;person.Sex 男;person.Age 61;person.Image new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() /Image/张学友.jpg));Persons.Add(person);return Persons;}}
}上图是我们重写控件的数据模板之后运行效果当然你也可以再加一些样式和触发器来美化效果当鼠标移动上去以及离开显示什么效果选中一行的时候显示什么效果都可以实现这也是WPF魅力所在以及强大之处!