Click here to Skip to main content
15,889,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to do a complex binding from a generics list??

XML
List<someclass> list = new List<someclass>();
grid1.DataContext = list;


ex


C#
public class someclass
{
    public string name { get; set; }
    public List<anotherclass> list{ get; set; }
}
public class anotherclass
{
    public string street { get; set; }

}
<grid name="grid1">
<listbox itemssource="{Binding}" displaymemberpath="name" /> // that one will work but if i want to bind the otherclass streets to another Listbox?
<listbox itemssource="{Binding Path=anotherclass}" displaymemberpath="street" />
That will work but i only get one item in my listbox? so how can i get all the items that my list contains?</grid>
Posted

This doesn't make much sense to me.
When you write this binding ItemsSource="{Binding Path=anotherclass}" you're saying that the item source must be the anotherclass proeprty of the object in the DataContext and since List<t></t> doesn't have such a property the ItemsSource will be set to null.

You need to clarify what it is you're trying to achieve. Click "Improve question" to add more info/details. And then post a comment to this solution, telling me you updated the question, that way I get a notification.
 
Share this answer
 
v3
I am going to assume that you want to bind the first listbox to the list of someClass, and that you want to bind the second listbox to the list of anotherClass held in the list of anotherClass of the selected item in the first listbox. To do this you are going to have to do several things. First off you are going to have to implement INotifyPropertyChanged interface in your window code behind like this:-

C#
public partial class MainWindow: Window, INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;

       public void NotifyPropertyChanged(String info)
       {
           PropertyChangedEventHandler handler = PropertyChanged;
           if (handler != null)
           {
               handler(this, new PropertyChangedEventArgs(info));
           }
       }


Then you are going to have to implement two properties in your code behind, the first to hold the list of someClass, and the other to hold the selected someClass. Both of these properties should call NotifyPropertyChanged in the setter like this:-

C#
someClass selectedClass;
        public someClass SelectedClass
        {
            get
            {
                return selectedClass;
            }
            set
            {
                selectedClass = value;
                NotifyPropertyChanged("SelectedClass");
            }
        }

        List<someClass> classList;
        public List<someClass> ClassList
        {
            get
            {
                return classList;
            }
            set
            {
                classList = value;
                NotifyPropertyChanged("ClassList");
            }
        }


Then after populating your lists in the constructor you need to set the datacontext of the whole window to this, and set both new properties appropriately like this:-

C#
public MainWindow()
        {
            InitializeComponent();
            List<someClass> items = new List<someClass>();
            for (int i = 0; i <= 4; i++)
            {
                List<anotherClass> classList = new List<anotherClass>();
                for (int j = 0; j <= 4; j++)
                {
                    anotherClass class1 = new anotherClass() { street = "Street " + i.ToString() + j.ToString() };
                    classList.Add(class1);
                }
                someClass myItem = new someClass() { name = "Item " + i.ToString(),  anotherClassList = classList };
                items.Add(myItem);
            }
            this.DataContext = this;
            ClassList = items;
            SelectedClass =ClassList[0];
        }


Then in your xaml, you can bind the listboxes like this:-

XML
<ListBox ItemsSource="{Binding Path=ClassList}" SelectedItem="{Binding Path=SelectedClass, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="name"/>
        <ListBox Grid.Row="1" ItemsSource="{Binding Path=SelectedClass.anotherClassList}" DisplayMemberPath="street"/>


That should give you the desired effect i think.
 
Share this answer
 
Comments
Sanjay J Patolia 7-Nov-11 7:35am    
Exactly...nice one.
Thank you Wayne Gaylard that works fine :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900