繁体   English   中英

WPF DataGrid在标题后面的代码中添加选择所有复选框

[英]WPF DataGrid Add select all checkbox in Header in code behind

我根据用户交互(按钮单击,复选框等)向WPF数据网格动态添加复选框列。 我还需要以编程方式向列标题添加“全选”复选框。 有什么想法怎么做?

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

我会通过XAML来完成,它应该看起来像这样

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

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

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

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实现):

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代码:

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);
        }
    }
}

结果如下: 截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM