繁体   English   中英

WPF ListBox中ItemTemplate和ItemContainerStyle有什么区别?

[英]What's the difference between ItemTemplate and ItemContainerStyle in a WPF ListBox?

在WPF Listbox ,我对这两个概念感到困惑: ItemTemplateItemContainerStyle有人能解释我更多吗?

ItemTemplate用于设置数据项内容的显示方式。 您可以使用它来绑定数据字段,格式化显示字符串等。 它决定了数据的呈现方式。

ItemContainerStyle用于样式化数据项的容器。 在列表框中,这将是一个ListBoxItem。 这里的样式会影响选择行为或背景颜色等内容。 它决定了显示器的风格和用户体验。

上面链接的ItemContainerStyle的MSDN页面有一个很好的例子,显示了一些差异:

  <!--Use the ItemTemplate to set a DataTemplate to define the visualization of the data objects. This DataTemplate specifies that each data object appears with the Proriity and TaskName on top of a silver ellipse.--> <ItemsControl.ItemTemplate> <DataTemplate> <DataTemplate.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="18"/> <Setter Property="HorizontalAlignment" Value="Center"/> </Style> </DataTemplate.Resources> <Grid> <Ellipse Fill="Silver"/> <StackPanel> <TextBlock Margin="3,3,3,0" Text="{Binding Path=Priority}"/> <TextBlock Margin="3,0,3,7" Text="{Binding Path=TaskName}"/> </StackPanel> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> <!--Use the ItemContainerStyle property to specify the appearance of the element that contains the data. This ItemContainerStyle gives each item container a margin and a width. There is also a trigger that sets a tooltip that shows the description of the data object when the mouse hovers over the item container.--> <ItemsControl.ItemContainerStyle> <Style> <Setter Property="Control.Width" Value="100"/> <Setter Property="Control.Margin" Value="5"/> <Style.Triggers> <Trigger Property="Control.IsMouseOver" Value="True"> <Setter Property="Control.ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content.Description}"/> </Trigger> </Style.Triggers> </Style> </ItemsControl.ItemContainerStyle> 

ItemContainerStyle只是DataTemplate的包装器,因此可以将公共项样式应用于不同的数据布局。

另外,从这个回答“DataTemplate vs ItemContainerStyle”

您可以在ItemTemplate中执行所有样式,但ItemContentStyle具有VisualStates,可控制鼠标悬停/禁用/选择等上的不透明度。

如果要更改这些不透明度状态更改,或者如果您想要除矩形之外的任何容器形状(例如三角形),则必须覆盖默认的ItemContainerStyle。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM