c# - 列表框中的 WPF 数据模板多态性

标签 c# wpf polymorphism datatemplate

`我有一个列表框,其中包含代表各种任务结果的项目。它们各自扩展了一个公共(public)基类“ResultsViewModel”,因此共享显示器所需的某些属性。

我想在资源字典中定义的是每个不同类型的任务结果的数据模板,例如任务A将有一个ResultsAViewModel的实现类,任务B将有ResultsBViewModel等。我想为每个不同类型的任务结果定义一个数据模板这些子类中的每一个都将 Listbox 的 ItemsSource 绑定(bind)到 ObservableCollection(父类),并使用 WPF 的多态性来确定在运行时使用哪个数据模板。复杂的是,根据各种触发器,它将成为为每个结果类型选择的三个数据模板之一,具体取决于结果是正在处理、已完成还是失败。所以每个派生类都会有模板。

到目前为止,我已经将通用样式应用到了列表框的样式,如下所示

<ListBox Background="{StaticResource AppBackground}" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Padding="10" Style="{StaticResource ResultsItemTemplate}" ItemsSource="{Binding Results}" MouseDoubleClick="ListBox_MouseDoubleClick" />

该样式如下

    <!-- Region General Results styles -->
<Style TargetType="{x:Type ListBox}" x:Key="ResultsItemTemplate" >
    <Setter Property="Background">
        <Setter.Value>
            Tan
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <ContentControl Content="{Binding}">
                    <ContentControl.Style>
                        <Style TargetType="{x:Type ContentControl}">
                            <Setter Property="ContentTemplate" Value="{DynamicResource CalculatingResultsTemplate}"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=ProcessingResult}" Value="1">
                                    <Setter Property="ContentTemplate" Value="{DynamicResource ProcessedResultsTemplate}"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=ProcessingResult}" Value="-1">
                                    <Setter Property="ContentTemplate" Value="{DynamicResource ErroredResultsTemplate}"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                                    <Setter Property="Background" Value="White"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ContentControl.Style>
                </ContentControl>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="{StaticResource AppBackground}" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True" >
                        <Setter Property="Background" Value="{StaticResource AppBackground}" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                </Style.Resources>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
</Style>

<!-- EndRegion -->

重要的是,您可以看到此样式根据触发器将 ItemTemplate 设置为三个值之一。这些模板键是

处理结果模板 计算结果模板 错误结果模板

每一个都是我的资源字典中的一个数据模板。

我遇到的问题是,对于每种派生 View 模型类型,我都需要上述三个数据模板中的每一个。我通过键引用它们。并且字典中不能有两个具有相同键的项目。例如,如果我使用键“ProcessedResultsTemplate”创建两个数据模板,其中一个数据模板的数据类型为 x:Type CalculationResultsViewModel,第二个数据模板的数据类型相同,但数据类型为 x:Type SpotStressResultsViewModel,则它将无法工作,因为两者具有相同的 key 。

所以我不确定实现这个的正确方法,以便在这里获得一些 WPF 多态性的乐趣。这是我做错的基本事情吗?

更新:我尝试使用数据模板选择器并使用下面的类,逻辑实现得很好,但问题是数据模板选择器仅在第一次渲染对象时才被调用。原始 XAML 具有在某些依赖项属性更改时更改数据模板的触发器。如何使用触发器或依赖属性来要求数据模板选择器从新数据值中重新选择数据模板?

class ResultsDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate CalculateOnlyProcessedTemplate { get; set; }
    public DataTemplate CalculateOnlyCalculatingTemplate { get; set; }
    public DataTemplate CalculateOnlyErroredTemplate { get; set; }

    public DataTemplate SpotStressProcessedTemplate { get; set; }
    public DataTemplate SpotStressCalculatingTemplate { get; set; }
    public DataTemplate SpotStressErroredTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is CalculationResultsViewModel)
        {
            var vm = item as CalculationResultsViewModel;

            if (vm.ProcessingResult == 1)
                return CalculateOnlyProcessedTemplate;

            if (vm.ProcessingResult == -1)
                return CalculateOnlyErroredTemplate;

            return CalculateOnlyCalculatingTemplate;
        }

        if (item is SpotStressResultsViewModel)
        {
            var vm = item as SpotStressResultsViewModel;

            if (vm.ProcessingResult == 1)
                return SpotStressProcessedTemplate;

            if (vm.ProcessingResult == -1)
                return SpotStressErroredTemplate;

            return SpotStressCalculatingTemplate;
        }

        return null;
    }
}

最佳答案

显而易见的解决方案是使用 DataTemplateSelector用于选择正确模板的任意复杂决策。将实际逻辑转移到 XAML 中会带来痛苦。

我个人认为 ViewModel 应该是 View 、它们的组合需求和它们的交互的直接、可测试和外观的表示。您演示的 ViewModel 似乎更像是一个业务模型对象,而不是表示 View 。您的 ViewModel 层应该负责生成隐含的:

  • 结果ACalculateViewModel
  • 结果AProcessingViewModel
  • 结果AErrorViewModel
  • 结果BCalculateViewModel
  • 结果BProcessingViewModel
  • 结果BErrorViewModel

您的 View 代码应该只是设计这些样式。多态性是一种更轻松地实现这些隐含 ViewModel 的好方法,但对于创建 UI 的人来说,这不应该是一个问题。

关于c# - 列表框中的 WPF 数据模板多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17885566/

相关文章:

c# - 使用泛型类型反射执行类

wpf - 从数据网格中删除命令不起作用

c# - 我无法使用 wpf 设置 Web 文本框值

list - 类型 ('a -> ' b) list 的函数 -> OCaml 中的 'a -> ' b list

c++是否可以将对象类型传递给要与之比较的函数

c# - VIEWS 和 Fluent NHibernate?

c# - 如何证明释放了弱引用?

c# - 如何绑定(bind) asp :chart (from microsoft) to a Dictionary<string, string>?

c# - WPF 文本框 - 德语字母 ß 自动替换为 ü

c# - 用派生类覆盖基类的属性