I am using MVVM, VS 2008 and .NET 3.5 SP1. I have a list of elements, each of which exposes the IsSelected property. I added a CheckBox to control the selection / deselection of all items in the list (updating each IsSelected property). Everything works, except that the IsChecked property is not updated in the view when the PropertyChanged event is fired for the associated CheckBox control.
<CheckBox Command="{Binding SelectAllCommand}" IsChecked="{Binding Path=AreAllSelected, Mode=OneWay}" Content="Select/deselect all identified duplicates" IsThreeState="True" />
My virtual machine:
public class MainViewModel : BaseViewModel { public MainViewModel(ListViewModel listVM) { ListVM = listVM; ListVM.PropertyChanged += OnListVmChanged; } public ListViewModel ListVM { get; private set; } public ICommand SelectAllCommand { get { return ListVM.SelectAllCommand; } } public bool? AreAllSelected { get { if (ListVM == null) return false; return ListVM.AreAllSelected; } } private void OnListVmChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "AreAllSelected") OnPropertyChanged("AreAllSelected"); } }
I am not showing an implementation of SelectAllCommand or individual elements here, but that does not seem to matter. When the user selects one item in the list (or clicks on the CheckBox problem to select / remove all items), I checked that the OnPropertyChanged ("AreAllSelected") line of code executes and monitors in the debugger, can see the PropertyChanged event is signed and fires as expected . But the getAllSelected get property is only executed once - when the view is actually displayed. The Visual Studio Output window does not report any data binding errors, so from what I can tell, the CheckBox IsSelected property is correctly bound.
If I replace CheckBox with a button:
<Button Content="{Binding SelectAllText}" Command="{Binding SelectAllCommand}"/>
and update the virtual machine:
... public string SelectAllText { get { var msg = "Select All"; if (ListVM != null && ListVM.AreAllSelected != null && ListVM.AreAllSelected.Value) msg = "Deselect All"; return msg; } } ... private void OnListVmChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "AreAllSelected") OnPropertyChanged("SelectAllText"); }
everything works as expected - the button text is updated as all elements are selected / disabled. Is there something I am missing in binding to the CheckBox IsSelected property?
Thanks for any help!
source share