如何绑定抽象类的继承类的属性

时间:2019-10-16 13:29:45

标签: c# wpf inheritance data-binding

我有一个带有一些继承类的抽象类:

public abstract class Base
{    
    public Base()
    {
    }
}

public class InheritedA : Base
{
    public bool PropertyA { get; set; }

    public InheritedA()
    {
    }
}

public class InheritedB : Base
{
    public bool PropertyB { get; set; }

    public InheritedB()
    {
    }
}

当在datacontext类中引用了“ Base”类时,如何在XAML中绑定继承类的PropertyA:

public class DataContextHere: INotifyPropertyChanged
{
    public Base baseClass{ get; set; }
}

类似吗?

<RadioButton IsChecked="{Binding baseClass.PropertyA}">

编辑奖金: 而且,是否只有布尔属性存在且为true时才可以绑定?

<RadioButton IsChecked="{Binding baseClass.PropertyA}">
<RadioButton IsChecked="{Binding baseClass.PropertyB}">

在这种情况下,如果在baseClass中引用了InheritedA,则检查第一个单选按钮;如果引用了InheritedA,则检查第二个单选按钮。

谢谢

1 个答案:

答案 0 :(得分:1)

与您一样,只要baseClass公共属性,它实际上返回一个Inherited实例:

<RadioButton IsChecked="{Binding baseClass.PropertyA}">

public Base baseClass { get; } = new Inherited();
相关问题