Template、ItemsPanel、ItemContainerStyle、ItemTemplate
2017-12-05 15:54
495 查看
它代表的是一个控件的内部结构组成部分(Visual Tree)。如:Button的默认Template外面是一个边框,里面是文字描述。如果我们要把普通的文字Button改为图片按钮的话就必须要重写Button的模板,要在里面加上一个Image,即已经把Button的Template修改了。在WPF中所有继承自contentcontrol类的控件都含有此属性,(继承自FrameworkElementdl类的TextBlock等控件无)。Template和Style有点容易混淆,每个控件初始没有Style属性,而在WPF中所有的控件都有默认的Template。Style改变的只是控件原来的属性,比如长宽颜色之类的,而Template可以改变控件的形状外形,还可以根据需要往里面添加其他的控件来丰富当前的控件。Style可以用来定义一定范围内的所有对应控件的样式,所以平时多为两者结合使用。
[html]
view plain
copy
print?
<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来实现横向排列或者其他更复杂的排列方式。
[html]
view plain
copy
print?
<ListBox >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
ItemContainerStyle是控件子项的样式,在ListBox里即ListBoxItem的Style属性,只是在ListBox设ItemContainerStyle表示当前控件的所有子项都默认了这个style,它的格式就是对应子项控件的Style。
[html]
view plain
copy
print?
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}">
<ListBoxItem />
<ListBoxItem />
</ListBox>
ItemTemplate是控件子项的显示数据的模板。与子项的Template属性等价,但更方便。
[html]
view plain
copy
print?
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="first"/>
<TextBlock Grid.Column="1" Text="second"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
[html]
view plain
copy
print?
<Style x:Key="ListBoxStyle" TargetType="ListBoxItem">
<Setter Property="Background" Value="#FFFFFFFF"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
//
//
</ControlTemplate>
</Setter.Value>
</Setter>
</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来实现横向排列或者其他更复杂的排列方式。
[html]
view plain
copy
print?
<ListBox >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<ListBox > <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox>
ItemContainerStyle是控件子项的样式,在ListBox里即ListBoxItem的Style属性,只是在ListBox设ItemContainerStyle表示当前控件的所有子项都默认了这个style,它的格式就是对应子项控件的Style。
[html]
view plain
copy
print?
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}">
<ListBoxItem />
<ListBoxItem />
</ListBox>
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"> <ListBoxItem /> <ListBoxItem /> </ListBox>
ItemTemplate是控件子项的显示数据的模板。与子项的Template属性等价,但更方便。
[html]
view plain
copy
print?
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="first"/>
<TextBlock Grid.Column="1" Text="second"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
相关文章推荐
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate (部分内容有待验证)
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate(包括ListBox的Item子项是横向排列)
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
- 项模板选择器属性(ItemTemplateSelector属性)和样式选择器(ItemContainerStyleSelector)
- 项模板选择器属性(ItemTemplateSelector属性)和样式选择器(ItemContainerStyleSelector)
- WPF Template,ItemsPresenter,ItemContainerStyle,ItemsPanel,contentpresenter
- WP7 ListBox中如何取得DateTemplate/ItemsPanelTemplate中的命名控件?
- WPF 深入浅出 模板(DataTemplate 数据外衣、ControlTemplate 控件外衣、ItemsPanelTemplate 项布局
- WPF:ListBox的默认ItemContainerStyle
- #455 – Using ItemContainerStyle to Bind Data Elements in a Collection to a Grid
- WPF为ItemsControl设置ItemsPanelTemplate
- WPF:ListBox 利用ItemContainerStyle改变ListBoxItem样式
- WPF ListBox ItemContainerStyle 设置BackGround 和 BorderBrush 无效
- ItemsControl的ItemContainerStyle属性
- ListBox Grouping的研究(二):ContainerStyle 与 GroupItem的绑定对象