通过命令(MVVM)

时间:2017-07-25 08:07:21

标签: c# wpf listview mvvm command

我的Window中有4个ListView,每个ListView都有一个CheckBox列,如下所示: enter image description here

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

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

但我也不想要有4种不同的选择" IsSelected"我的ViewModel中的属性,然后我可以像这个帖子中建议的那样绑定到列表视图的样式:Select All items in ListView with MVVM

还有其他方法吗?是否可以将ListView Control作为命令参数发送?

我试过了:

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

但是我的命令中的参数始终为null。我猜我的WPF技能有点生疏......

2 个答案:

答案 0 :(得分: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访问视图元素。

答案 1 :(得分: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>