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

时间:2015-02-08 10:36:20

标签: c# wpf xaml checkbox datagrid

我根据用户交互(按钮点击,复选框等)向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>

1 个答案:

答案 0 :(得分:1)

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

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

这就是结果: Screenshot