WPF ListBox 中的 ItemTemplate 和 ItemContainerStyle 有什么区别? [英] What's the difference between ItemTemplate and ItemContainerStyle in a WPF ListBox?

查看:22
本文介绍了WPF ListBox 中的 ItemTemplate 和 ItemContainerStyle 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WPF Listbox 中,我对以下两个概念感到困惑:ItemTemplateItemContainerStyle谁能给我解释一下?

解决方案

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

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

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

<块引用>

 <!--使用ItemTemplate设置一个DataTemplate来定义数据对象的可视化.这个数据模板指定每个数据对象以优先级出现和 TaskName 在银色椭圆的顶部.--><ItemsControl.ItemTemplate><数据模板><数据模板.资源><样式 TargetType="TextBlock"><Setter 属性="FontSize" 值="18"/><Setter Property="Horizo​​ntalAlignment" Value="Center"/></风格></DataTemplate.Resources><网格><椭圆填充=银"/><堆栈面板><TextBlock 边距="3,3,3,0"Text="{绑定路径=优先级}"/><TextBlock 边距="3,0,3,7"文本="{绑定路径=任务名称}"/></堆栈面板></网格></数据模板></ItemsControl.ItemTemplate><!--使用ItemContainerStyle属性指定外观包含数据的元素.This ItemContainerStyle给每个项目容器一个边距和一个宽度.还有一个触发器,它设置一个显示描述的工具提示鼠标悬停在项目容器上时的数据对象.--><ItemsControl.ItemContainerStyle><风格><Setter 属性="Control.Width" 值="100"/><Setter Property="Control.Margin" Value="5"/><风格.触发器><触发属性="Control.IsMouseOver" 值="True"><Setter 属性="控制工具提示"Value="{绑定RelativeSource={x:静态RelativeSource.Self},Path=Content.Description}"/></触发器></Style.Triggers></风格></ItemsControl.ItemContainerStyle>

In WPF Listbox, I'm confused with these 2 notions: ItemTemplate and ItemContainerStyle Can someone explain me more?

解决方案

The ItemTemplate is for styling how the content of your data item appears. You use it to bind data fields, format display strings, and so forth. It determines how the data is presented.

The ItemContainerStyle is for styling the container of the data item. In a list box, this would be a ListBoxItem. Styling here affects things like selection behavior or background color. It determines style and UX of the display.

The MSDN page for ItemContainerStyle, linked above, has a pretty good example showing some differences:

 <!--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>

这篇关于WPF ListBox 中的 ItemTemplate 和 ItemContainerStyle 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆