2017-07-25 82 views
1

我在窗口4名列表视图,每个ListView中都有一个复选框列像这样: enter image description here选择ListView中的所有项目,通过命令(MVVM)

现在我想实现1个命令,我可以绑定到我有每个ListView的标题中的CheckBox。因此,如果单击标题中的复选框,它将选择该ListView中的所有项目,如果再次单击,它将再次取消选中它们。

我知道通过代码背后的点击事件很容易做到这一点,但我不认为这符合MVVM,是吗?

但我也不想在我的视图模型4个不同的“IsSelected”属性,我可以再结合就像是有人在列表视图的风格在这个职位建议:Select All items in ListView with MVVM

是否有任何其他方式?是否有可能发送ListView控件作为命令参数?

我试过:

<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>--> 

但参数在我的命令总是空。我WPF技能有点生疏我猜...

+0

要什么属性DemandLicence是复选框约束? –

+1

如果你将你的参数绑定到“DemandLicenses”,那么你可以迭代你的viewmodel并选择它们? – GCamel

+0

@RomanoZumbé它尚未绑定到任何属性,因为我不需要绑定它。 – Whatever

回答

2

你应该只设置对象的财产DemandLicenses绑定到行级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>--> 

查看模型

// 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"); 
    } 
} 

这样,您不必将命令绑定到CheckBox,也不需要访问ViewModel中的视图元素。

+0

好的,但那正是我不想做的。因为我有** 4个不同的ListView **,然后我需要** 4种不同的属性**。 我的意思是如果没有其他解决方案,我将不得不这样做。但我希望有更好的办法。还是我理解你错了? – Whatever

0

又快又脏?

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; 
    } 

与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>