Tom
Tom

Reputation: 97

How to bind WPF value to a class within a class

I am trying to bind a value to a class within a class.

<Textbox Text="{Binding Path=Height}" />

public partial class Test : Page
{
    Builder builder = new Builder();
    public Test()
    {
        InitializeComponent();
        DataContext = builder;
    }
}

public class Builder
{
    public AnotherClass Height { get; set; }
}

public class AnotherClass
{
    public String Feet { get; set; }
    public String Inches { get; set; }
}

I would have thought that binding to Height.Feet would then update the value within the object but the object just gets set to null.

Upvotes: 0

Views: 58

Answers (1)

T-X
T-X

Reputation: 165

You are instantiating a Builder but you do not initialize its Height property. That is why the binding source property is null.

It could look like

public AnotherClass Height { get; set; } = new AnotherClass();

Also initiate both properties of AnotherClass because String defaults to null.

Upvotes: 2

Related Questions