首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据农业中的SelectAll

数据农业中的SelectAll
EN

Stack Overflow用户
提问于 2014-01-25 21:46:39
回答 1查看 699关注 0票数 0

我找到了一个很好的SelectAll复选框解决方案,它只使用XAML:

代码语言:javascript
复制
<DataGrid x:Name="TestGrid" Tag="false">
   <DataGrid.Resources> 
       <DataTemplate x:Key="HeaderCheckbox">
           <CheckBox Name="SelectAll" IsChecked="{Binding RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType=DataGrid}, Path=Tag, Mode=TwoWay}" /> 
       </DataTemplate>    
       <DataTemplate x:Key="ItemCheckbox"> 
           <CheckBox IsChecked="{Binding RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType=DataGrid}, Path=Tag, Mode=OneWay}" /> 
       </DataTemplate> 
   </DataGrid.Resources> 
   <DataGrid.Columns> 
       <DataGridTemplateColumn HeaderTemplate="{StaticResource HeaderCheckbox}" CellTemplate="{StaticResource ItemCheckbox}" /> 
       <DataGridTextColumn Binding="{Binding FirstName}" /> 
   </DataGrid.Columns> 
 </DataGrid>

来源:数据网格中SelectAll的完全XAML解决方案

但这是我的问题。在上面的示例中, ItemCheckbox 绑定到Datagrid的属性,然后如何将ItemCheckbox绑定到数据字段 inclusive

EN

回答 1

Stack Overflow用户

发布于 2014-01-26 00:06:55

最简单的解决方案是在视图模型中使用属性而不是Tag。首先,在包含项的类中,创建SelectAll属性,该属性将在更改时相应地更新所有项:

代码语言:javascript
复制
public class MyItemCollection : INotifyPropertyChanged 
{
    private readonly ObservableCollection<MyItem> _items;

    public ICollection<MyItem> Items { get { return _items; } }

    private bool _selectAll;

    public bool SelectAll
    {
        get { return _selectAll; }
        set
        {
            if (_selectAll != value)
            {
                _selectAll = value;
                OnPropertyChanged("SelectAll");
                foreach (var item in _items) item.IsSelected = value;
            }
        }
    }
}

然后将IsSelected属性添加到项中。它将由SelectAll属性或DataGrid中的CheckBox更新。

代码语言:javascript
复制
public class MyItem : INotifyPropertyChanged 
{
    private bool _isSelected;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (_isSelected != value)
            {
                _isSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }
    }
}

然后更新绑定以指向新属性:

代码语言:javascript
复制
<DataGrid x:Name="TestGrid" ItemsSource="{Binding Items}">
   <DataGrid.Resources>
      <DataTemplate x:Key="HeaderCheckbox">
         <CheckBox Name="SelectAll" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.SelectAll}" />
      </DataTemplate>
      <DataTemplate x:Key="ItemCheckbox">
         <CheckBox IsChecked="{Binding Path=IsSelected}" />
      </DataTemplate>
   </DataGrid.Resources>

</DataGrid>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21356662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档