【问题标题】:Select All items in ListView, via Command (MVVM)通过命令 (MVVM) 选择 ListView 中的所有项目
【发布时间】:2017-12-31 00:48:11
【问题描述】:

我的窗口中有 4 个 ListView,每个 ListView 都有一个 CheckBox 列,如下所示:

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

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

但我也不想在我的 ViewModel 中有 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 技能有点生疏......

【问题讨论】:

  • 复选框绑定到 DemandLicence 的哪个属性?
  • 如果您将参数绑定到“DemandLicenses”,那么您可以迭代您的视图模型并选择它们?
  • @RomanoZumbé 它还没有绑定到任何属性,因为我还不需要绑定它。
  • 这对我来说似乎不是很 MVVM。如果复选框未绑定到任何属性,您只能通过直接与其交互来设置它的选中状态。这不是 MVVM 方法
  • @GCamel 好主意!但是我的“DemandLicenses”没有属性“isSelected”,我可以设置为 true 或类似的东西,但我可以为此添加一个属性。

标签: c# wpf listview mvvm command


【解决方案1】:

您应该只设置绑定到行级别CheckBoxDemandLicenses 中对象的属性

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 访问视图元素。

【讨论】:

  • 嗯,是的,但这正是我不想做的。因为我有 4 个不同的 ListViews,然后我需要有 4 个不同的属性。我的意思是,如果没有其他解决方案,我将不得不这样做。但我希望有更好的方法。还是我理解错了?
【解决方案2】:

又快又脏?

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2016-02-06
    • 2020-08-04
    相关资源
    最近更新 更多