Search code examples
c#wpfinheritancedata-binding

How to bind a property of inherited class from abstract class


I have an abstract class with some inherited classes:

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()
    {
    }
}

How can I bind in the XAML the PropertyA of inherited class when I just have a "Base" class referenced in the datacontext class:

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

Something similar to this?:

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

Edited Bonus: And, Is it possible to bind only if the boolean property exists and is true?

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

In this case, it is checked the first radiobutton if InheritedA is referenced in baseClass, or the second one if InheritedA is referenced.

Thank you


Solution

  • Like you do, provided that baseClass is a public property that actually returns an Inherited instance:

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

    public Base baseClass { get; } = new Inherited();