0

Here is what I consider the relevant part of the ListView in question:

<ListView ItemsSource="{Binding People}" >
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.HeaderTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding AllSelected}" Command="{Binding ToggleSelectAll}"  CommandParameter="{Binding}" />
                    </DataTemplate>
                </GridViewColumn.HeaderTemplate>
                </GridViewColumn.HeaderTemplate>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsSelected}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

The first column is thus a checkbox bound to the IsSelected property of the row item, a PickListPerson.

The command is declared in the PersonPickListViewModel as follows:

public DelegateCommand<PersonPickListViewModel> ToggleSelectAll { get; set; } = new DelegateCommand<PersonPickListViewModel>(vm =>
    {
        vm.AllSelected = !vm.AllSelected;
        vm.People.ToList().ForEach(p => p.IsSelected = vm.AllSelected);
    },
    vm => true
);

I pass the viewmodel as the command parameter so as to have access to instance properties of the viewmodel in the static context of the command's action delegate.

Yet when I click the header column checkbox, this command is not invoked. Why could that be?

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • Missing OnPropertyChanged inside IsSelected and AllSelected? Maybe too obvious to check. – smartobelix Nov 19 '16 at 06:41
  • I have that on `AllSelected', but `IsSelected` belongs to each member of an `ObservableCollection`, and I expect that collection to handle change notifications for what it is bound to. – ProfK Nov 19 '16 at 09:31
  • ObservableCollection raises events automatically but only for adding/removing elements. Still for changing properties inside objects in collection you need OnPropertyChanged notification. – smartobelix Nov 19 '16 at 17:37

1 Answers1

2

The DataContext at the point you bind the CommandParamter is the item from the People collection. I'm guessing this isn't the PersonPickListViewModel you're expecting. As whatever this type is can't be cast to PersonPickListViewModel the command doesn't get invoked.

Edit after first comment: If your PersonPickListViewModel is at the root of the window, you can use "DataContext Anchoring" to get to it. Simply add a name to your Window (i.e. x:Name="view") and then update your binding to use an ElementName source as follows:

{Binding ElementName=view, Path=DataContext}

ibebbs
  • 1,963
  • 2
  • 13
  • 20
  • Yes, you are correct. The `PersonPickListViewModel` is the data context for the whole view. How could I specify that as the data context for the header template? I have it specified in the view using ``. – ProfK Nov 19 '16 at 09:36
  • I have expanded the answer to show how this can be achieved. – ibebbs Nov 19 '16 at 10:32
  • Thank you very much. Gaarg, A whole project without using names, and now I need one. I suppose just one is fine. – ProfK Nov 19 '16 at 15:31