zoukankan      html  css  js  c++  java
  • wpf中使用mvvm,解决checkbox全选问题

    <DataGrid Name="dataGrid" IsReadOnly="False" Width="600" Height="400" AutoGenerateColumns="False" ItemsSource="{Binding GeneryModelList}" >
    <DataGrid.Columns>
    <DataGridTemplateColumn Width="150">
    <DataGridTemplateColumn.Header>
    <CheckBox Command="{ Binding DataContext.SelectOrUnSelectAll,RelativeSource={RelativeSource AncestorType=DataGrid}}" Content="全选"></CheckBox>
    </DataGridTemplateColumn.Header>
    <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <CheckBox IsChecked="{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></CheckBox>
    </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    开始原因是由于并不知道使用数据模板,需要加入数据源,要找到数据源的作用域。后面的命令部分代码如下:

    public ICommand SelectOrUnSelectAll
    {
    get { return new GeneryModelSoft.Commands.Command(p => SelectAll()); }
    }

    protected void SelectAll()
    {

    if (generyModelList != null&&isSelectFlag==false)
    {
    for (int i = 0; i < generyModelList.Count; i++)
    {
    generyModelList[i].IsSelected = true;
    }
    isSelectFlag = true;
    }
    else if (generyModelList != null && isSelectFlag == true)
    {
    for (int i = 0; i < generyModelList.Count; i++)
    {
    generyModelList[i].IsSelected = false;
    }
    isSelectFlag = false;
    }


    }

  • 相关阅读:
    C#
    数据库SQL Server
    JavaScript题目
    vscode: Visual Studio Code 常用快捷键
    jQuery教程
    JavaScript快速排序
    JS编程艺术
    JS
    linux 笔记
    积累的各种资源
  • 原文地址:https://www.cnblogs.com/sdnyzhl/p/2986381.html
Copyright © 2011-2022 走看看