在wpf中显示继承对象的最佳方法是什么?

时间:2022-04-04 15:55:10

Lets say I have this:

让我说我有这个:

public class Result 
{
   public bool Success { get; set; }
   public string Description { get; set; }
}

Then I want to add another level, like this:

然后我想添加另一个级别,如下所示:

public class AssertionFailedResult : Result
{
  public string Expected { get; set; }
  public string Actual { get; set; }
}

In WPF, how would I show the simple result one way and the assertion failed result another way? I'd like to basically make a template based on type.

在WPF中,我将如何以单向方式显示简单结果,并以另一种方式导致断言失败?我想基本上根据类型制作模板。

2 个解决方案

#1


If you create a DataTemplate in a resource dictionary and set the DataType property, but don't set the x:Key property, the framework will associate the DataTemplate to objects based on the object's runtime type. For better or worse, inheritance has no effect. In other words, even if you didn't have a template where the DataType was "AssertionFailedResult", the framework would not bind objects of type "AssertionFailedResult" to a template where the DataType was "Result".

如果在资源字典中创建DataTemplate并设置DataType属性,但不设置x:Key属性,则框架将根据对象的运行时类型将DataTemplate与对象关联。无论好坏,继承都没有效果。换句话说,即使您没有DataType为“AssertionFailedResult”的模板,框架也不会将“AssertionFailedResult”类型的对象绑定到DataType为“Result”的模板。

EDIT: Sorry, I got it backwards. DataTemplates do have a "polymorphic" behavior. Styles do not. In any case, the frameworks should bind to the DataTemplate with the more specific DataType.

编辑:对不起,我倒退了。 DataTemplates确实具有“多态”行为。样式没有。在任何情况下,框架都应该使用更具体的DataType绑定到DataTemplate。

#2


I took Daniel's answer and made an example out of it. I thought posting the code might be helpful:

我接受了丹尼尔的回答并做了一个例子。我认为发布代码可能会有所帮助:

<Window x:Class="SampleWpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:SampleWpfApplication="clr-namespace:SampleWpfApplication">
    <Window.Resources>
        <DataTemplate DataType="{x:Type SampleWpfApplication:Result}">
            <Label>Simple Result</Label>            
        </DataTemplate>
        <DataTemplate DataType="{x:Type SampleWpfApplication:AssertionFailedResult}">
            <Label>Assertion Failed!</Label>
        </DataTemplate>
    </Window.Resources>
    <ContentControl x:Name="contentControl" Content="{Binding Path=Result}" />
</Window>

Next, a model class that is the data context of the window:

接下来,作为窗口数据上下文的模型类:

public class Model
{
    public Result Result { get; set; }
}

And in the MainWindow, I set the DataContext as follows:

在MainWindow中,我将DataContext设置如下:

DataContext = new Model()
                  {
                      Result = new AssertionFailedResult()
                                   {
                                       Success = false,
                                       Description = "Assertion failed",
                                       Expected = "1",
                                       Actual = "1"
                                   }
                  };

So with the DataTemplate, wpf knows how to render the control without any additional direction on my part. Thanks again, Daniel.

因此,使用DataTemplate,wpf知道如何在没有任何额外指示的情况下渲染控件。再次感谢,丹尼尔。

#1


If you create a DataTemplate in a resource dictionary and set the DataType property, but don't set the x:Key property, the framework will associate the DataTemplate to objects based on the object's runtime type. For better or worse, inheritance has no effect. In other words, even if you didn't have a template where the DataType was "AssertionFailedResult", the framework would not bind objects of type "AssertionFailedResult" to a template where the DataType was "Result".

如果在资源字典中创建DataTemplate并设置DataType属性,但不设置x:Key属性,则框架将根据对象的运行时类型将DataTemplate与对象关联。无论好坏,继承都没有效果。换句话说,即使您没有DataType为“AssertionFailedResult”的模板,框架也不会将“AssertionFailedResult”类型的对象绑定到DataType为“Result”的模板。

EDIT: Sorry, I got it backwards. DataTemplates do have a "polymorphic" behavior. Styles do not. In any case, the frameworks should bind to the DataTemplate with the more specific DataType.

编辑:对不起,我倒退了。 DataTemplates确实具有“多态”行为。样式没有。在任何情况下,框架都应该使用更具体的DataType绑定到DataTemplate。

#2


I took Daniel's answer and made an example out of it. I thought posting the code might be helpful:

我接受了丹尼尔的回答并做了一个例子。我认为发布代码可能会有所帮助:

<Window x:Class="SampleWpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:SampleWpfApplication="clr-namespace:SampleWpfApplication">
    <Window.Resources>
        <DataTemplate DataType="{x:Type SampleWpfApplication:Result}">
            <Label>Simple Result</Label>            
        </DataTemplate>
        <DataTemplate DataType="{x:Type SampleWpfApplication:AssertionFailedResult}">
            <Label>Assertion Failed!</Label>
        </DataTemplate>
    </Window.Resources>
    <ContentControl x:Name="contentControl" Content="{Binding Path=Result}" />
</Window>

Next, a model class that is the data context of the window:

接下来,作为窗口数据上下文的模型类:

public class Model
{
    public Result Result { get; set; }
}

And in the MainWindow, I set the DataContext as follows:

在MainWindow中,我将DataContext设置如下:

DataContext = new Model()
                  {
                      Result = new AssertionFailedResult()
                                   {
                                       Success = false,
                                       Description = "Assertion failed",
                                       Expected = "1",
                                       Actual = "1"
                                   }
                  };

So with the DataTemplate, wpf knows how to render the control without any additional direction on my part. Thanks again, Daniel.

因此,使用DataTemplate,wpf知道如何在没有任何额外指示的情况下渲染控件。再次感谢,丹尼尔。