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:-
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:-
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:-
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:-
<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.