WPF DataGrid在Header代码中添加select all复选框 [英] WPF DataGrid Add select all checkbox in Header in code behind

查看:451
本文介绍了WPF DataGrid在Header代码中添加select all复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据用户交互(按钮点击,复选框等),将动态复选框列添加到WPF datagrid。我还需要通过编程方式添加一个全选复选框。任何想法如何做?

I am adding dynamically checkbox columns to WPF datagrid based on user interactions (button clicks, checkboxes etc). I need also to add an Select all checkbox to column header programmatically. Any ideas how to do it?

 DataGridCheckBoxColumn  checkBoxColumn=new DataGridCheckBoxColumn();
 checkBoxColumn.Header = "Title";
 checkBoxColumn.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
 checkBoxColumn.Binding = new Binding(e.PropertyName);
 checkBoxColumn.IsThreeState = true;

我会通过XAML做它应该看起来像这样

I would do it through XAML it should look something like this

<DataGridTemplateColumn>
            <DataGridTemplateColumn.Header>
                <CheckBox isChecked="{Binding SelectAll}"></CheckBox>
            </DataGridTemplateColumn.Header>
            <DataGridTemplateColumn.CellTemplate >
                <DataTemplate >
                    <CheckBox IsChecked="{Binding MyRowCheckProperty}"></CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>


推荐答案

也许我不明白你的问题,但是你意思是这样吗?

Maybe I did not understand your question, but do you mean something like this?

Binding binding = new Binding("SelectAll");
binding.Mode = BindingMode.TwoWay;

CheckBox headerCheckBox = new CheckBox();
headerCheckBox.Content = "Title";
headerCheckBox.SetBinding(CheckBox.IsCheckedProperty, binding);

DataGridCheckBoxColumn checkBoxColumn = new DataGridCheckBoxColumn();
checkBoxColumn.Header = headerCheckBox;
checkBoxColumn.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
checkBoxColumn.Binding = new Binding(e.PropertyName);
checkBoxColumn.IsThreeState = true;

我希望如此。

编辑

您的评论中描述的行为是奇怪的。但是这是完整的项目(我使用的是.NET 4.0)。

It is strange the behavoiur that you describe in your comment. However this is the complete project (I used .NET 4.0).

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="600">
    <StackPanel>
        <DataGrid Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Mode=OneWay, Path=Parties}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*" />
                <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" Width="*" />
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>

型号:

namespace WpfApplication1
{
    public class Party : INotifyPropertyChanged
    {
        private bool isSelected;
        private string name;
        private string surname;

        public event PropertyChangedEventHandler PropertyChanged;

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

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        public string Surname
        {
            get
            {
                return surname;
            }
            set
            {
                if (surname != value)
                {
                    surname = value;
                    OnPropertyChanged("Surname");
                }
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

ViewModel (您可以通过创建基类来改进 INotifyPropertyChanged 实现):

ViewModel (you can improve the INotifyPropertyChanged implementation, by creating a base class):

namespace WpfApplication1
{
    public class ViewModel : INotifyPropertyChanged
    {
        private bool selectAll;
        private readonly ObservableCollection<Party> parties = new ObservableCollection<Party>();

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public ObservableCollection<Party> Parties
        {
            get
            {
                return parties;
            }
        }

        public bool SelectAll
        {
            get
            {
                return selectAll;
            }
            set
            {
                if (selectAll != value)
                {
                    selectAll = value;
                    OnPropertyChanged("SelectAll");
                    SelectAllImpl(selectAll);
                }
            }
        }

        private void SelectAllImpl(bool isSelected)
        {
            foreach (Party party in parties)
            {
                party.IsSelected = isSelected;
            }
        }
    }
}

那么XAML代码在后面:

And then XAML code behind:

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ViewModel viewModel = new ViewModel();
            viewModel.Parties.Add(new Party() { Name = "Joe", Surname = "Redgate" });
            viewModel.Parties.Add(new Party() { Name = "Mike", Surname = "Blunt" });
            viewModel.Parties.Add(new Party() { Name = "Gina", Surname = "Barber" });

            DataContext = viewModel;

            Binding binding = new Binding("DataContext.SelectAll");
            binding.Mode = BindingMode.TwoWay;
            binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor);
            binding.RelativeSource.AncestorType = GetType();

            CheckBox headerCheckBox = new CheckBox();
            headerCheckBox.Content = "Is Selected";
            headerCheckBox.SetBinding(CheckBox.IsCheckedProperty, binding);

            DataGridCheckBoxColumn checkBoxColumn = new DataGridCheckBoxColumn();
            checkBoxColumn.Header = headerCheckBox;
            checkBoxColumn.Binding = new Binding("IsSelected");


            dataGrid.Columns.Insert(0, checkBoxColumn);
        }
    }
}

这是结果:

And this is the result:

这篇关于WPF DataGrid在Header代码中添加select all复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆