locked
Data Binding for the Nested Class Objects RRS feed

  • Question

  •  

    Hi,

     

    I have the following class designed for holding data.

     

    class DataBindingData

    {

    private int data1;

    private int data2;

     

    //Nested Class Object

    public DataNested dataNested;

     

    public int Data1

    {

    set {data1 = value;}

    get {return data1;}

    }

     

    public int Delta2

    {

    set {data2 = value;}

    get {return data2;}

    }

     

    public class DataNested

    {

    private string data1;

     

    public string Data1

    {

    set {data1 = value;}

    get {return data1;}

    }

    }

     

    }

     

    };

     

     

    When using XAML, following is the case,

     

    <StackPanel Name="RootElem">

    <StackPanel.Resources>

    <cBig SmileataBindingData x:Key="myDataSource"/>

    </StackPanel.Resources>

    <StackPanel.DataContext>

    <Binding Source="{StaticResource myDataSource}"/>

    </StackPanel.DataContext>

     

    <TextBox Name="TextBoxTest"

    Text="{Binding Path=dataNested.Data1}" />

     

     

    The Text Box above does not show the dataNested.Data1.

     

    Can somebody help to achieve data binding with NestedObjects from XAML?

     

    I have work arounnds like - setting DataContext explicitly for TextBox or metioning explicit source but I would like to know if somehow I can use nested data for binding?

     

    Thanks in advance.

    Tuesday, August 28, 2007 11:23 AM

Answers

  • it should be exposed as public property

    private DataNested _dn;

    public DataNested MyDataNested

    {

    get { return _dn; }

    set { _dn = value; }

    }

     

    Then you can do

    <TextBlock Text="{Binding Path=MyDataNested.Data1}"></TextBlock>

    Tuesday, August 28, 2007 12:30 PM

All replies

  • it should be exposed as public property

    private DataNested _dn;

    public DataNested MyDataNested

    {

    get { return _dn; }

    set { _dn = value; }

    }

     

    Then you can do

    <TextBlock Text="{Binding Path=MyDataNested.Data1}"></TextBlock>

    Tuesday, August 28, 2007 12:30 PM
  • Hi Lee,

    It worked, thanks for timely help.

     

    Thanks and Regards,

    Anil

     

    Wednesday, August 29, 2007 6:18 AM