1

I have 4 ListViews in my Window, every ListView has a CheckBox column Like this: enter image description here

Now I want to implement 1 Command, which I can bind to the CheckBox in the header of each ListView I have. So if the CheckBox in the header is clicked it will select all items in that ListView and if clicked again, it will unselect them all again.

I know it would be easy to do that via a click event in code behind, but i don't think that is MVVM conform, is it?

But I also don't want to have 4 different "IsSelected" Properties in my ViewModel which I could then bind to the style of the listview like someone suggested in this post: Select All items in ListView with MVVM

Is there any other way? Is it maybe possible to send the ListView Control as command parameter?

I tried that:

  <ListView x:Name="UserDemandListView" Grid.Column="2" Grid.Row="2" MinWidth="200" ItemsSource="{Binding DemandLicenses}" Grid.RowSpan="2">
         <ListView.View>
              <GridView>
                  <!--<SnippetGridViewColumnCheckBox>-->
                    <GridViewColumn CellTemplate="{StaticResource FirstCell}" Width="25">
                       <CheckBox x:Name="CheckAll3" Content="" Command="{Binding SelectAllCommand}" CommandParameter="{Binding ElementName=UserDemandListView}" Margin="4,0,0,0"/>
                    </GridViewColumn>
                    <!--</SnippetGridViewColumnCheckBox>-->

But the Parameter in my command is always null. My WPF skills are a bit rusty I guess ...

Soheil Alizadeh
  • 2,936
  • 11
  • 29
  • 56
Whatever
  • 77
  • 1
  • 12
  • To what property of the DemandLicence is the checkBox bound? – Romano Zumbé Jul 25 '17 at 08:10
  • 1
    if you bind your parameter to the "DemandLicenses", then you can iterate your viewmodel and select them ? – GCamel Jul 25 '17 at 08:11
  • @RomanoZumbé It is not bound to any Property yet, because I don't need to bind it yet. – Whatever Jul 26 '17 at 09:24
  • 1
    That does not seem very MVVM to me. If the checkbox is not bound to any property you can only set it's checked state by directly interacting with it. That is not an MVVM approach – Romano Zumbé Jul 26 '17 at 09:30
  • @GCamel Very good Idea! But my "DemandLicenses" has no property "isSelected" which I could set on true or something like that, but I could add a property for that. – Whatever Jul 26 '17 at 09:34
  • @RomanoZumbé yes you might be right, considering GCamel answer, it might be good to make a Proptery which i can bind. I will try that thanks. – Whatever Jul 26 '17 at 09:39

2 Answers2

2

You should just set the property of the objects in DemandLicenses that is bound to the row level CheckBox

XAML

<ListView x:Name="UserDemandListView" Grid.Column="2" Grid.Row="2" MinWidth="200" ItemsSource="{Binding DemandLicenses}" Grid.RowSpan="2">
    <ListView.View>
        <GridView>
            <!--<SnippetGridViewColumnCheckBox>-->
            <GridViewColumn CellTemplate="{StaticResource FirstCell}" Width="25">
                <CheckBox x:Name="CheckAll3" Content=""  Margin="4,0,0,0" Checked={Binding CheckAllDemandLicenses}"/>
            </GridViewColumn>
            <!--</SnippetGridViewColumnCheckBox>-->

View Model

// Property, that shows if all Items need to be checked
private bool _checkAllDemandLicenses;
public bool CheckAllDemandLicenses
{
    get
    {
        return _checkAllDemandLicenses;
    }
    set
    {
        _checkAllDemandLicenses = value;

        foreach(DemandLicense d in DemandLicenses)
        {
            // Set the property, that is bound to the row level checkbox
            d.Selected = value;
        }

        OnPropertyChanged("CheckAllDemandLicenses"); // Or whatever your implementation for INotifyPropertyChanged is
        OnPropertyChanged("DemandLicenses");
    }
}

This way, you don't have to bind a command to the CheckBox and also you don't need to access view elements from your ViewModel.

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • Well yes, but that is exactly what I don't wanted to do. Because I have **4 different ListViews** and then I would need to have **4 different Propertys**. I mean if there is no other solution I will have to make it like that. But I was hoping for a better way. Or do I understand you wrong? – Whatever Jul 26 '17 at 09:21
0

quick and dirty ?

private void chkAll_Checked(object sender, RoutedEventArgs e)
    {
        if ((sender as CheckBox).Name == "chkMailAll")
            foreach (SupEquipementViewModel c in _dataGrid.ItemsSource)
                c.EnvoiMail = 1;

        if ((sender as CheckBox).Name == "chkActiveAll")
            foreach (SupEquipementViewModel c in _dataGrid.ItemsSource)
                c.Actif = 1;

        if ((sender as CheckBox).Name == "chkRemoveAll")
            foreach (SupEquipementViewModel c in _dataGrid.ItemsSource)
                c.Supprime = 1;
    }

with the xaml

<DataGridTemplateColumn Width="Auto" CanUserSort="True" CanUserResize="True">
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="chkMailAll" Content="{DynamicResource String.EquipmentView.CheckEnvoiMail}"
                                  Checked="chkAll_Checked" Unchecked="chkAll_Unchecked" />
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding EnvoiMail,UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Width="Auto" CanUserSort="True" CanUserResize="True">
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="chkActiveAll" Content="{DynamicResource String.EquipmentView.CheckActif}"
                                  Checked="chkAll_Checked" Unchecked="chkAll_Unchecked" />
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Actif,UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Width="Auto" CanUserSort="True" CanUserResize="True">
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="chkRemoveAll" Content="{DynamicResource String.EquipmentView.CheckDeleted}"
                                  Checked="chkAll_Checked" Unchecked="chkAll_Unchecked" />
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Supprime,UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
GCamel
  • 612
  • 4
  • 8