I have a this structure in XAML where every item has checkbox.
□ Fruits
□ Apple
□ Banana
□ Orange
My goal is to have a function that if Fruit's checkbox is checked, it will check all (select all) fruits.
So I am binding my ViewModel's function
in my Checkbox's IsChecked
.
<TreeView
ItemsSource="{Binding Foods}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type domain:Food}" ItemsSource="{Binding Fruits}">
<StackPanel Orientation="Horizontal">
<CheckBox
IsChecked="{Binding SelectAll}"
/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type domain:Fruit}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</TreeView.Resources>
</TreeView>
My ViewModel is:
private bool _selectAll = false;
public bool SelectAll
{
get
{
return _selectAll;
}
set
{
foreach (var item in Fruits)
{
item.isSelected = true;
}
}
}
Edit
public class Food
{
public string Name { get; set; }
public ObservableCollection<Fruit> Fruits { get; set; } = new ObservableCollection<Fruit>();
}
public class Fruit
{
public string Name { get; set; }
public bool IsSelected { get; set; }
}
But nothing's happening.