I have a WPF app and i work there with MVVM. I have a ViewModel where i hold 2 ObservableCollection private members (Test is a class that i created). My MainWindow has a TabControl, each tab is connected to another ObservableCollection.
The MVVM works in the first load (If the ObservableCollection item's isChecked is true - the checkBox is checked, and if not - the checkBox is unchecked).
I added a checkBox for SelectAll or UnselectAll. When clicking on it - I go to the specific ObservableCollection and change all the IsChecked members accordingly to the SelectAll checkBox. The peoblem is that i don't see it in the GUI (the first load is keeping there, and change just if i click on each checkBox manually)
Should i "refresh" the GUI somehow? Thanks.
The ViewModel.cs
private ObservableCollection<Test> _basicTests;
private ObservableCollection<Test> _advancedTests;
public ObservableCollection<Test> BasicTests
{
get { return _basicTests; ; }
set
{
_basicTests = value;
OnPropertyChanged("BasicTests");
}
}
public ObservableCollection<Test> AdvancedTests
{
get { return _advancedTests; ; }
set
{
_advancedTests = value;
OnPropertyChanged("AdvancedTests");
}
}
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
}
The MainWindow.xaml:
<TabControl x:Name="tabControl" Margin="0,135,0,0" TabStripPlacement="Left">
<TabItem Header="Basic">
<ListView x:Name="basicTestsList" ItemsSource="{Binding BasicTests}" Background="#FFE5E5E5">
<ListView.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="cbListOfBasicTests"
IsChecked="{Binding IsChecked}"
Content="{Binding Name}"
Margin="0,5,0,0"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</TabItem>
<TabItem Header="Advanced">
<ListView x:Name="advancedTestsList" ItemsSource="{Binding AdvancedTests}" Background="#FFE5E5E5">
<ListView.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="cbListOfAdvancedTests"
IsChecked="{Binding IsChecked}"
Content="{Binding Name}"
Margin="0,5,0,0"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</TabItem>
</TabControl>
Test.cs:
class Test
{
public string Name { get; set; }
public bool IsChecked { get; set; }
}