I have two custom ListBox
inside my UserControl
.
My custom ListBox1
has a private List<Element> SubList
which is a sub-list of its elements. This custom control implements the PropertyChanged
of INotifyPropertyChanged
etc etc.
My ListBox2
has to display this sub-list.
Both of them are also using DataTemplate
but I do not think it will be the problem here so I will not take care of this part here. If I am wrong, let me know and I will update my example.
<UserControl>
<local:ListBox1 x:Name="ListBox1"
DataContext="{Binding MyFullList}"/>
<local:ListBox2 x:Name="ListBox2"
DataContext="{Binding ElementName=ListBox1}"
Content="{Binding Path=SubList}"/>
</UserControl>
It's giving me inside my ListBox2:
(Collection)
I tried using just one element instead of the list of elements and it was working. I also tried like this:
<UserControl>
<local:ListBox1 x:Name="ListBox1"
DataContext="{Binding MyFullList}"/>
<local:ListBox2 x:Name="ListBox2"
DataContext="{Binding ElementName=ListBox1, Path=SubList}"/>
</UserControl>
But it is giving me nothing... I also tried using ObservableCollection
instead of List
but still nothing.
What should be my Binding to fill ListBox2
with the SubList
of ListBox1
?
What am I doing wrong ?
I found the solution to my problem. I tried using a list already filled and it was working. So the problem was during the update of the ListBox2
and not the DataBinding
.
I do not know why but the List
named SubList
was not updating the ListBox2
even if it was triggering the OnPropertyChanged
event, etc.
I just needed to change my SubList
to an ObservableCollection
and the following code is doing the DataBinding
correctly :
<UserControl>
<local:ListBox1 x:Name="ListBox1"
DataContext="{Binding MyFullList}"/>
<local:ListBox2 x:Name="ListBox2"
DataContext="{Binding ElementName=ListBox1, Path=SubList}"/>
</UserControl>