Question

I am trying to bind a label to an Object.Object.Property and I don't get it run.

Here is my code:

XAML

<Window x:Class="MyApp.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MyWindow" Height="1120" Width="800">
    <Grid Name="MyGrid">
        <StackPanel Orientation="Horizontal">
            <Label FontWeight="Bold" FontSize="40" Content="{Binding MyDataObject/AnotherSubObject/MyProperty}"/>
        </StackPanel>
    </Grid>
</Window>

And the Code:

public partial class MyWindow : Window
{
    public MySubObject MyDataObject { get; set; }

    public MyWindow(MySubObject object)
    {
        InitializeComponent();

        this.MyDataObject = object; // Contains MyDataObject.AnotherObject.MyProperty

        DataContext = this;
    }
}

And the code for MySubObject object looks like this:

    public class MySubObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    AnotherObject _AnotherObject;

    public MySubObject()
    {
        this._AnotherObject = new AnotherObject();
        this._AnotherObject.Property = "Some Value";
    }

    public AnotherObject AnotherObject
    {
        get { return _AnotherObject; }
        set { _AnotherObject = value; OnPropertyChanged("AnotherObject"); }
    }

    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

I would be glad to get dome support for this case.

Was it helpful?

Solution

Use Dot(.) as binding property path separator not Forward slash(/)

<Label FontWeight="Bold" FontSize="40"
       Content="{Binding MyDataObject.AnotherSubObject.MyProperty}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top