1
votes

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; }
}
2
The ObservableCollection will only raise a OnPropertyChanged event when you add or remove items. If you want one when you change the IsChecked property, you need to add it in there too.Robin Bennett
implement OnPropertyChanged for IsChecked and Name propertiesSasha

2 Answers

2
votes

The ObservableCollection will only raise a OnPropertyChanged event when you add or remove items. If you want the UI to update when you change the IsChecked property, Class Test needs to raise the OnPropertyChanged event when IsChecked is changed:

class Test
{
    private bool isChecked;
    public bool IsChecked
    {
        get { return this.isChecked; }
        set
        {
            this.isChecked= value;
            this.OnPropertyChanged("IsChecked");
        }
    }
}
0
votes

If you don't want to change the model and add OnPropertyChanged, you can do something like this:

_basicTests = new ObservableCollection(_basicTests.ForEach(bt => bt.IsChecked = true));