Template、ItemsPanel、ItemContainerStyle、ItemTemplate

它代表的是一个控件的内部结构组成部分(Visual Tree)。如:Button的默认Template外面是一个边框,里面是文字描述。如果我们要把普通的文字Button改为图片按钮的话就必须要重写Button的模板,要在里面加上一个Image,即已经把Button的Template修改了。在WPF中所有继承自contentcontrol类的控件都含有此属性,(继承自FrameworkElementdl类的TextBlock等控件无)。Template和Style有点容易混淆,每个控件初始没有Style属性,而在WPF中所有的控件都有默认的Template。Style改变的只是控件原来的属性,比如长宽颜色之类的,而Template可以改变控件的形状外形,还可以根据需要往里面添加其他的控件来丰富当前的控件。Style可以用来定义一定范围内的所有对应控件的样式,所以平时多为两者结合使用。
  1. <Style x:Key="ListBoxStyle" TargetType="ListBoxItem">    
  2. <Setter Property="Background" Value="#FFFFFFFF"/>    
  3. <Setter Property="Template">    
  4. <Setter.Value>    
  5. <ControlTemplate TargetType="ListBoxItem">    
  6. //   
  7. //    
  8. </ControlTemplate>    
  9. </Setter.Value>    
  10. </Setter>    
  11. </Style>    
<Style x:Key="ListBoxStyle" TargetType="ListBoxItem">  
<Setter Property="Background" Value="#FFFFFFFF"/>  
<Setter Property="Template">  
<Setter.Value>  
<ControlTemplate TargetType="ListBoxItem">  
// 
//  
</ControlTemplate>  
</Setter.Value>  
</Setter>  
</Style>  

  • ItemsPanel是指控件的子项的布局样式,只有那些有item的控件才有此属性,如ListBox ,Combox,TreeView,DataGrid,TabelControl等,后面的两个也是如此。eg:在不做设置的时候,ListBox的Item子项是纵向排列的,但是可以通过设置ItemPanel来实现横向排列或者其他更复杂的排列方式。

  1. <ListBox >      
  2. <ListBox.ItemsPanel>    
  3. <ItemsPanelTemplate>    
  4.   <VirtualizingStackPanel Orientation="Horizontal"/>   
  5. </ItemsPanelTemplate>    
  6.   </ListBox.ItemsPanel>    
  7. </ListBox>    
<ListBox >    
<ListBox.ItemsPanel>  
<ItemsPanelTemplate>  
  <VirtualizingStackPanel Orientation="Horizontal"/> 
</ItemsPanelTemplate>  
  </ListBox.ItemsPanel>  
</ListBox>  


  • ItemContainerStyle是控件子项的样式,在ListBox里即ListBoxItem的Style属性,只是在ListBox设ItemContainerStyle表示当前控件的所有子项都默认了这个style,它的格式就是对应子项控件的Style。
  1. <ListBox ItemContainerStyle="{StaticResource  ListBoxItemStyle}">       
  2. <ListBoxItem />    
  3. <ListBoxItem />    
  4. </ListBox>    
<ListBox ItemContainerStyle="{StaticResource  ListBoxItemStyle}">     
<ListBoxItem />  
<ListBoxItem />  
</ListBox>  
  • ItemTemplate是控件子项的显示数据的模板。与子项的Template属性等价,但更方便。
  1. <Setter Property="ItemTemplate">  
  2.     <Setter.Value>  
  3.         <DataTemplate>  
  4.             <Grid>  
  5.                 <Grid.ColumnDefinitions>  
  6.                     <ColumnDefinition Width="*"/>  
  7.                     <ColumnDefinition Width="3"/>  
  8.                 </Grid.ColumnDefinitions>  
  9.                 <TextBlock Grid.Column="0" Text="first"/>  
  10.                 <TextBlock Grid.Column="1" Text="second"/>  
  11.             </Grid>  
  12.          </DataTemplate>  
  13.     </Setter.Value>  
  14. </Setter>  
<ItemsControl x:Class="HDSProject.Control.CustomFont" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HDSProject.Control" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="itemCtrl" ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}" FontSize="20" Foreground="Teal" > <ItemsControl.ItemContainerStyle> <Style TargetType="ContentPresenter"> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel> <Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}"> <TextBlock Text="{Binding}" /> </Border> <Rectangle Height="1" Fill="{Binding Foreground, ElementName=itemCtrl}" Margin="0,0,0,5"> <Rectangle.Style> <Style TargetType="Rectangle"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True"> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </Rectangle.Style> </Rectangle> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>给这个自定义控件添加Thumb
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值