马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
一、界说:
Template:在WPF中,Template通常指的是控件的模板,用于界说控件的表面和布局。每个控件都有一个默认的模板,但我们可以通过自界说模板来完全改变控件的表面,而不改变其举动。Template通常通过设置控件的Template属性来指定。
- ControlTemplate:控件模板,用于界说控件的视觉布局和表面。
- DataTemplate:数据模板,用于界说数据对象的视觉表现。
- ItemsPanelTemplate:项面板模板,用于界说ItemsControl的布局面板。
二、使用
- 数据模板:数据内容的表现情势(决定命据表面)
ataTemplate 通常用于内容控件(如ContentControl)或列表控件(如ListBox、ComboBox)中,以指定怎样表现数据对象。
- <Window.Resources>
- <DataTemplate x:Key = "carInfoTemplate" DataType="{x:Type local:Person}">
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
- <TextBlock Text="{Binding Age}" Margin="10,0,0,0"/>
- </StackPanel>
- </DataTemplate>
- </Window.Resources>
- <UserControl ContentTemplate="{StaticResource carInfoTemplate}"/>
复制代码
- 控件模板:算法内容的表现情势(决定控件表面):
- ControlTemplate 通过界说一组元素(如Border、Grid、ContentPresenter等)来构建控件的视觉树。ControlTemplate不改变控件的逻辑举动,只改变其表面。
- ItemsPanelTemplate :用于界说ItemsControl(如ListBox、ItemsControl)的项布局面板。默认环境下,ItemsControl使用StackPanel作为项布局面板,但你可以通过ItemsPanelTemplate将其改为其他面板,如WrapPanel、UniformGrid等。
- <ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
- <Grid>
- <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
- <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
- </Grid>
- </ControlTemplate>
复制代码- <ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
- <Grid>
- <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
- <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
- </Grid>
- </ControlTemplate>
复制代码 三、拓展
- ContentPresenter(内容出现器): 是一个用于表现内容的控件。它通常在ControlTemplate中使用,作为占位符,表现控件的内容应该表现的位置。ContentPresenter会自动应用相应的DataTemplate来表现内容。
- <ControlTemplate TargetType="Button">
- <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
- <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
- </Border>
- </ControlTemplate>
复制代码
- DataTemplate中的DataContext:
常见标题:在DataTemplate中绑定的下令,全部来自于ViewModel。但是这些下令却始终无法见效,调试发现这些下令根本没有绑定上。
办理方案:由于DataTemplate中无法访问到DataTemplate控件层以外的资源,包罗ViewModel中的下令。为了访问到ViewModel中的下令,我们必要改变下令源,通过指定父级来访问全局的DataContext。- <Window.Resources>
- <DataTemplate x:Key = "carInfoTemplate" DataType="{x:Type local:Person}">
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
- <TextBlock Text="{Binding Age}" Margin="10,0,0,0"/>
- </StackPanel>
- </DataTemplate>
- </Window.Resources>
- <UserControl ContentTemplate="{StaticResource carInfoTemplate}"/><ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
- <Grid>
- <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
- <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
- </Grid>
- </ControlTemplate><ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
- <Grid>
- <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
- <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
- </Grid>
- </ControlTemplate>
复制代码 四、使用场景
- 当你想要改变一个控件的表面(如按钮、文本框)时,使用ControlTemplate。
- 当你想要自界说数据对象在界面上的表现方式时,使用DataTemplate。
- 当你想要改变一个ItemsControl的项布局方式时,使用ItemsPanelTemplate。
- 当你在ControlTemplate中必要表现控件的内容时,使用ContentPresenter。
- <Window.Resources>
- <DataTemplate x:Key = "carInfoTemplate" DataType="{x:Type local:Person}">
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
- <TextBlock Text="{Binding Age}" Margin="10,0,0,0"/>
- </StackPanel>
- </DataTemplate>
- </Window.Resources>
- <UserControl ContentTemplate="{StaticResource carInfoTemplate}"/> <Window.Resources>
- <DataTemplate x:Key = "carInfoTemplate" DataType="{x:Type local:Person}">
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
- <TextBlock Text="{Binding Age}" Margin="10,0,0,0"/>
- </StackPanel>
- </DataTemplate>
- </Window.Resources>
- <UserControl ContentTemplate="{StaticResource carInfoTemplate}"/><ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
- <Grid>
- <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
- <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
- </Grid>
- </ControlTemplate><ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
- <Grid>
- <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
- <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
- </Grid>
- </ControlTemplate>
复制代码 |