Bogey
Bogey

Reputation: 5764

WPF - How to databind subclass property correctly

I have a probably very stupid problem with Databinding in WPF. I have a custom exception of type

public class MyException : Exception
{
    public class ThrowingMethod
    {
        public class RegisteredMethod
        {
            public readonly string registeredName;

            // ...
        }

        public readonly RegisteredMethod regMethod;

        // ...
    }

    public readonly ThrowingMethod throwingMethod;
    // ...
}

Right, now in my wpf window, I do

public partial class ExceptionDisplay : Window
{
    private readonly IEnumerable<MyException> exceptions;

    public ExceptionDisplay(IEnumerable<MyException> exceptions)
    {
        InitializeComponent();
        this.exceptions = exceptions;

    }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        DataContext = this.exceptions.First();
    }
}

And I have two labels in XAML:

<Label Name="Message" Content="{Binding Message}"/>

<Label Name="RegMethod" Content="{Binding Path=throwingMethod.regMethod.registeredName, Mode=OneWay}"/>

oddly, Message binds correctly, but the second label remains empty and nothing seems to bind to it. When I set the values manually by code in Window_Loaded_1, it works just fine, so the objects are all initialized correctly.

Why does throwingMethod.regMethod.registeredName not bind, am I doing something wrong here?

Thanks!

Upvotes: 0

Views: 1347

Answers (1)

Fratyx
Fratyx

Reputation: 5797

You just can bind to properties not fields of a class.

You at least have to write:

public ThrowingMethod throwingMethod { get; private set; }

Same for registeredName and regMethod.

Next question is if you need updating. So INotifyPropertyChanged interface would be useful.

Upvotes: 1

Related Questions