根据项目类型和视图选项切换 ListBox ItemTemplate [英] Switching ListBox ItemTemplate based on both item type and view option

查看:17
本文介绍了根据项目类型和视图选项切换 ListBox ItemTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 WPF 中实现一个列表框,它将为其项目提供 2 种替代布局:

I'm currently implementing a listbox in WPF that will have 2 alternative layouts for its items:

到目前为止,我已经使用 DataTriggerListBox 切换 ItemTemplate 并且效果很好:

So far, I've done this using a DataTrigger to switch the ItemTemplate for the ListBox and it's working well:

<ListBox ItemsSource="{Binding Runs}" SelectedItem="{Binding SelectedRun}">
  <ListBox.Style>
    <Style TargetType="ListBox">
      <Setter Property="ItemTemplate" Value="{StaticResource tileTemplate}"/>
      <Style.Triggers>
        <DataTrigger Binding="{Binding ShowRunsAsIcons}" Value="True">
          <Setter Property="ItemTemplate" Value="{StaticResource iconTemplate}"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ListBox.Style>
</ListBox>

然而,列表绑定的Runs集合也会包含不同的types对象:

However, the Runs collection to which the list is bound will also contain different types of object:

interface IRunItem
{
  // ...
}

class CompletedRunItem : IRunItem
{
  // ...
}

class PendingRunItem : IRunItem
{
  // ...
}

每种对象类型都应该有自己的平铺"和图标"模板(总共有 4 个模板).根据布尔 ShowRunsAsIcons 和列表项的类型切换这两个属性的最佳方式是什么?

Each of the object types should have its own 'tile' and 'icon' templates (making 4 templates in total). What's the best way of switching on these two properties to according to the boolean ShowRunsAsIcons and the type of the list item?

我考虑过使用一对 DataTemplateSelector 子类——一个用于根据项目类型在图块模板之间进行选择,一个用于根据项目类型在图标模板之间进行选择——但这只是感觉非常笨重.我觉得我应该利用 WPF 根据对象类型选择正确模板的能力,但在这种情况下,我不知道如何将它与列表的不同视图选项结合起来.

I've considered using a pair of DataTemplateSelector subclasses -- one to choose between tile templates based on item type, and one to choose between icon templates based on item type -- but this just feels horribly clunky. I feel as though I should be taking advantage of WPF's ability to choose the correct template based on the object's type, but in this instance, I don't see how to combine that with the list's different view options.

关于如何做到这一点更符合 WPF 精神的任何想法?

Any ideas of how to do this that's more in the spirit of WPF?

谢谢.

推荐答案

虽然我不相信这是最好的答案,但我已经改变了我的方法来利用 WPF 的自动模板选择.我现在为每个具体数据类定义了顶级"数据模板.

Although I'm not convinced it's the best answer, I've changed my approach to take advantage of WPF's automatic template selection. I now have 'top-level' data templates defined for each of my concrete data classes.

这些数据模板只包含一个 ContentControl,其 ContentTemplate 属性通过 DataTrigger 设置,绑定到数据上下文的 ShowRunsAsIcons 属性.

These data templates contain nothing but a ContentControl whose ContentTemplate property is set via a DataTrigger, binding to the data context's ShowRunsAsIcons property.

例如,这是 PendingRunItem 的无密钥数据模板:

As an example, here's the keyless data template for PendingRunItem:

<DataTemplate DataType="{x:Type Common:PendingRunItem}">
  <ContentControl Content="{Binding}">
    <ContentControl.Style>
      <Style TargetType="ContentControl">
        <Setter Property="ContentTemplate" Value="{StaticResource pendingTileTemplate}"/>
        <Style.Triggers>
          <DataTrigger Binding="{Binding DataContext.ShowRunsAsIcons, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}}" Value="True">
            <Setter Property="ContentTemplate" Value="{StaticResource pendingIconTemplate}"/>
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </ContentControl.Style>
  </ContentControl>
</DataTemplate>

相关类的图标和磁贴表示只是常规数据模板.ListBox 不再需要定义它的 Style 属性:

The icon and tile representations for the relevant classes are then just regular data templates. And the ListBox no longer needs its Style property defined:

<ListBox ItemsSource="{Binding Runs}" SelectedItem="{Binding SelectedRun}"/>

我很想知道人们对这种方法的看法,以及与使用一两个 DataTemplateSelector 相比它的优缺点.

I'd be interested to know people's thoughts on this approach and its benefits and drawbacks when compared to using a DataTemplateSelector or two.

这篇关于根据项目类型和视图选项切换 ListBox ItemTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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