• <i id='q1mT3'><tr id='q1mT3'><dt id='q1mT3'><q id='q1mT3'><span id='q1mT3'><b id='q1mT3'><form id='q1mT3'><ins id='q1mT3'></ins><ul id='q1mT3'></ul><sub id='q1mT3'></sub></form><legend id='q1mT3'></legend><bdo id='q1mT3'><pre id='q1mT3'><center id='q1mT3'></center></pre></bdo></b><th id='q1mT3'></th></span></q></dt></tr></i><div id='q1mT3'><tfoot id='q1mT3'></tfoot><dl id='q1mT3'><fieldset id='q1mT3'></fieldset></dl></div>

    1. <legend id='q1mT3'><style id='q1mT3'><dir id='q1mT3'><q id='q1mT3'></q></dir></style></legend>
        • <bdo id='q1mT3'></bdo><ul id='q1mT3'></ul>

        <small id='q1mT3'></small><noframes id='q1mT3'>

        <tfoot id='q1mT3'></tfoot>

        根据项目类型和视图选项切换 ListBox ItemTemplate

        时间:2023-10-06

        <small id='78qHZ'></small><noframes id='78qHZ'>

            <tbody id='78qHZ'></tbody>
            <bdo id='78qHZ'></bdo><ul id='78qHZ'></ul>
          • <i id='78qHZ'><tr id='78qHZ'><dt id='78qHZ'><q id='78qHZ'><span id='78qHZ'><b id='78qHZ'><form id='78qHZ'><ins id='78qHZ'></ins><ul id='78qHZ'></ul><sub id='78qHZ'></sub></form><legend id='78qHZ'></legend><bdo id='78qHZ'><pre id='78qHZ'><center id='78qHZ'></center></pre></bdo></b><th id='78qHZ'></th></span></q></dt></tr></i><div id='78qHZ'><tfoot id='78qHZ'></tfoot><dl id='78qHZ'><fieldset id='78qHZ'></fieldset></dl></div>

            <tfoot id='78qHZ'></tfoot>

            1. <legend id='78qHZ'><style id='78qHZ'><dir id='78qHZ'><q id='78qHZ'></q></dir></style></legend>
                  本文介绍了根据项目类型和视图选项切换 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有方法重载和枚举的奇怪(可能是错误的?)C# 编译器行为 下一篇:如何为 ListBoxItem 创建标题?

                  相关文章

                    <bdo id='DZR2t'></bdo><ul id='DZR2t'></ul>
                  1. <small id='DZR2t'></small><noframes id='DZR2t'>

                  2. <tfoot id='DZR2t'></tfoot>
                  3. <legend id='DZR2t'><style id='DZR2t'><dir id='DZR2t'><q id='DZR2t'></q></dir></style></legend>
                    <i id='DZR2t'><tr id='DZR2t'><dt id='DZR2t'><q id='DZR2t'><span id='DZR2t'><b id='DZR2t'><form id='DZR2t'><ins id='DZR2t'></ins><ul id='DZR2t'></ul><sub id='DZR2t'></sub></form><legend id='DZR2t'></legend><bdo id='DZR2t'><pre id='DZR2t'><center id='DZR2t'></center></pre></bdo></b><th id='DZR2t'></th></span></q></dt></tr></i><div id='DZR2t'><tfoot id='DZR2t'></tfoot><dl id='DZR2t'><fieldset id='DZR2t'></fieldset></dl></div>