WPF 模板Template

[复制链接]
发表于 2025-10-14 13:32:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
一、界说:
Template:在WPF中,Template通常指的是控件的模板,用于界说控件的表面和布局。每个控件都有一个默认的模板,但我们可以通过自界说模板来完全改变控件的表面,而不改变其举动。Template通常通过设置控件的Template属性来指定。

  • ControlTemplate:控件模板,用于界说控件的视觉布局和表面。
  • DataTemplate:数据模板,用于界说数据对象的视觉表现。
  • ItemsPanelTemplate:项面板模板,用于界说ItemsControl的布局面板。
二、使用
   


  • 数据模板:数据内容的表现情势(决定命据表面)ataTemplate 通常用于内容控件(如ContentControl)或列表控件(如ListBox、ComboBox)中,以指定怎样表现数据对象。
  1. <Window.Resources>
  2.     <DataTemplate x:Key = "carInfoTemplate" DataType="{x:Type local:Person}">
  3.         <StackPanel Orientation="Horizontal">
  4.             <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
  5.             <TextBlock Text="{Binding Age}" Margin="10,0,0,0"/>
  6.         </StackPanel>
  7.     </DataTemplate>
  8. </Window.Resources>
  9. <UserControl ContentTemplate="{StaticResource carInfoTemplate}"/>   
复制代码

  • 控件模板:算法内容的表现情势(决定控件表面):

    • ControlTemplate 通过界说一组元素(如Border、Grid、ContentPresenter等)来构建控件的视觉树。ControlTemplate不改变控件的逻辑举动,只改变其表面。
    • ItemsPanelTemplate :用于界说ItemsControl(如ListBox、ItemsControl)的项布局面板。默认环境下,ItemsControl使用StackPanel作为项布局面板,但你可以通过ItemsPanelTemplate将其改为其他面板,如WrapPanel、UniformGrid等。

  1. <ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
  2.     <Grid>
  3.         <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
  4.         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
  5.     </Grid>
  6. </ControlTemplate>
复制代码
  1. <ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
  2.     <Grid>
  3.         <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
  4.         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
  5.     </Grid>
  6. </ControlTemplate>            
复制代码
三、拓展

  • ContentPresenter(内容出现器): 是一个用于表现内容的控件。它通常在ControlTemplate中使用,作为占位符,表现控件的内容应该表现的位置。ContentPresenter会自动应用相应的DataTemplate来表现内容。
  1. <ControlTemplate TargetType="Button">
  2.     <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
  3.         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
  4.     </Border>
  5. </ControlTemplate>
复制代码

  • DataTemplate中的DataContext:
   常见标题:在DataTemplate中绑定的下令,全部来自于ViewModel。但是这些下令却始终无法见效,调试发现这些下令根本没有绑定上。
     办理方案:由于DataTemplate中无法访问到DataTemplate控件层以外的资源,包罗ViewModel中的下令。为了访问到ViewModel中的下令,我们必要改变下令源,通过指定父级来访问全局的DataContext。
  1. <Window.Resources>
  2.     <DataTemplate x:Key = "carInfoTemplate" DataType="{x:Type local:Person}">
  3.         <StackPanel Orientation="Horizontal">
  4.             <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
  5.             <TextBlock Text="{Binding Age}" Margin="10,0,0,0"/>
  6.         </StackPanel>
  7.     </DataTemplate>
  8. </Window.Resources>
  9. <UserControl ContentTemplate="{StaticResource carInfoTemplate}"/><ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
  10.     <Grid>
  11.         <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
  12.         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
  13.     </Grid>
  14. </ControlTemplate><ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
  15.     <Grid>
  16.         <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
  17.         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
  18.     </Grid>
  19. </ControlTemplate>   
复制代码
四、使用场景

  • 当你想要改变一个控件的表面(如按钮、文本框)时,使用ControlTemplate。
  • 当你想要自界说数据对象在界面上的表现方式时,使用DataTemplate。
  • 当你想要改变一个ItemsControl的项布局方式时,使用ItemsPanelTemplate。
  • 当你在ControlTemplate中必要表现控件的内容时,使用ContentPresenter。
  1. <Window.Resources>
  2.     <DataTemplate x:Key = "carInfoTemplate" DataType="{x:Type local:Person}">
  3.         <StackPanel Orientation="Horizontal">
  4.             <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
  5.             <TextBlock Text="{Binding Age}" Margin="10,0,0,0"/>
  6.         </StackPanel>
  7.     </DataTemplate>
  8. </Window.Resources>
  9. <UserControl ContentTemplate="{StaticResource carInfoTemplate}"/>   <Window.Resources>
  10.     <DataTemplate x:Key = "carInfoTemplate" DataType="{x:Type local:Person}">
  11.         <StackPanel Orientation="Horizontal">
  12.             <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
  13.             <TextBlock Text="{Binding Age}" Margin="10,0,0,0"/>
  14.         </StackPanel>
  15.     </DataTemplate>
  16. </Window.Resources>
  17. <UserControl ContentTemplate="{StaticResource carInfoTemplate}"/><ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
  18.     <Grid>
  19.         <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
  20.         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
  21.     </Grid>
  22. </ControlTemplate><ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
  23.     <Grid>
  24.         <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/>
  25.         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
  26.     </Grid>
  27. </ControlTemplate>
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表