Add a checkbox in WPF datagrid header and use it to select/unselect all checkboxes in DataGridCheckBoxColumn

[亡魂溺海] 提交于 2019-12-14 03:59:47

问题


I am new to WPF and would appreciate help on following problem.

In my wpf datagrid I have DataGridCheckBoxColumn as first column and I have bind this column to IsSelected property in ViewModel.

<toolkit:DataGridCheckBoxColumn Header="Title" Binding="{Binding isSelected}"/>

I also want to have a checkbox in header row and which I intend to use to select/unselect all the checkboxes in this column.

Till now I have managed to get a checkbox in header by applying a headerstyle as shown in below code snip but I am not able to toggle selection for all the checkboxes in column

<Style x:Key="CheckBoxHeaderStyle" TargetType="{x:Type toolkit:DataGridColumnHeader}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type toolkit:DataGridColumnHeader}">
                <CheckBox x:Name="chkToggleSelection"   VerticalAlignment="Center">
                </CheckBox>
            </ControlTemplate>
        </Setter.Value>
     </Setter>
 </Style>

回答1:


You will have to handle the Checkbox.Click event (if you are using MVVM then set the CheckBox.Command or use attached behavior to handle the event) and then set the boolean property of all items bound to the datagrid as true \ false accordingly.

Sadly there is no other alternative that I am aware of!




回答2:


<DataGrid x:Name="DgLines" ItemsSource="{Binding OpcUaEndpoints}" AutoGenerateColumns="False"
                     SelectionMode="Extended"  IsReadOnly="True" Grid.ColumnSpan="5">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn>
                                <DataGridTemplateColumn.HeaderTemplate>
                                <DataTemplate>
                                    <CheckBox Name="ckbSelectedAll" 
                                          IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}">
                                            <i:Interaction.Triggers>
                                                <i:EventTrigger EventName="Checked" >
                                                    <i:InvokeCommandAction Command="{Binding DataContext.CheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
                                                </i:EventTrigger>
                                                <i:EventTrigger EventName="Unchecked" >
                                                    <i:InvokeCommandAction Command="{Binding DataContext.UncheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
                                                </i:EventTrigger>
                                            </i:Interaction.Triggers>
                                        </CheckBox>
                                </DataTemplate>
                                </DataGridTemplateColumn.HeaderTemplate>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Name="cbkSelect" 
                                          IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                            <!--<DataGridTextColumn Width="200" Header="Id" Binding="{Binding Id }" />-->

                            <DataGridTextColumn Width="200" Header="Name" Binding="{Binding Name}"/>
                            <DataGridTextColumn Width="500" Header="Description" Binding="{Binding Description}"/>
                            <DataGridTextColumn Width="500" Header="Lines" Binding="{Binding Endpoint}"/>
                        </DataGrid.Columns>
                    </DataGrid>


        public class OpcUaEndpointsListViewModel : INotifyPropertyChanged
            {
                private static OpcUaEndpointsListViewModel _instance;
                private static readonly object Padlock = new object();
                private ICommand _addCommand;
                //private ICommand _uncheckCommand;
                private ICommand _searchcommand;
                private ICommand _checkedCommand { get; set; }
                private ICommand _unCheckedCommand { get; set; }

                private ObservableCollection<AddOpcUaEndpointsViewModel> _endpoint;
                public string _charNameFromTB;

                public event PropertyChangedEventHandler PropertyChanged;

                public OpcUaEndpointsListViewModel()
                {
                    BindDataGrid();
                }

                public static OpcUaEndpointsListViewModel Instance
                {
                    get
                    {
                        lock (Padlock)
                        {
                            return _instance ?? (_instance = new OpcUaEndpointsListViewModel());
                        }
                    }
                }

                /// <summary>
                ///     //OPC UA Endpoint List
                /// </summary>
                public ObservableCollection<AddOpcUaEndpointsViewModel> OpcUaEndpoints
                {
                    get { return _endpoint; }
                    set
                    {
                        if (OpcUaEndpoints == value)
                        {
                            _endpoint = value;
                            OnPropertyChanged("OpcUaEndpoints");
                        }
                    }
                }

                public string CharNameFromTB
                {
                    get { return _charNameFromTB; }
                    set
                    {
                        _charNameFromTB = value;
                        OnPropertyChanged("CharNameFromTB");
                    }
                }

                public ICommand AddCommand
                {
                    get { return _addCommand ?? (_addCommand = new WpfApplication1.RelayCommand<object>(p => ExecuteAddCommand())); }
                }

                public ICommand SearchCommand
                {
                    get { return _searchcommand ?? (_searchcommand = new RelayCommand<object>(p => ExecuteSearchCommand())); }
                }


                public ICommand CheckedCommand
                {
                    get { return _checkedCommand ?? (_checkedCommand = new WpfApplication1.RelayCommand<object>(p => ExecuteCheckedCommand())); }
                }
                public ICommand UncheckedCommand
                {
                    get { return _unCheckedCommand ?? (_unCheckedCommand = new WpfApplication1.RelayCommand<object>(p => ExecuteUnCheckedCommand())); }
                }


                private void ExecuteSearchCommand()
                {
                    ///BindDataGridsearch();
                }

                private void ExecuteCheckedCommand()
                {
                    foreach (var item in _endpoint)
                    {
                        item.IsSelected = true;
                    }
                }

                private void ExecuteUnCheckedCommand()
                {
                    foreach (var item in _endpoint)
                    {
                        item.IsSelected = false;
                    }
                }

                private void ExecuteAddCommand()
                {
                    ///BindDataGridsearch();
                }

                private void BindDataGrid()
                {
                    _endpoint = new ObservableCollection<AddOpcUaEndpointsViewModel>();
                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "A", Description = "A", Endpoint = 1 });
                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "B", Description = "B", Endpoint = 2 });
                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "C", Description = "C", Endpoint = 3 });
                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "D", Description = "D", Endpoint = 4 });
                    _endpoint.Add(new AddOpcUaEndpointsViewModel { Name = "E", Description = "E", Endpoint = 5 });
                }

                public void OnPropertyChanged(string propertyName)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }
            }

public class AddOpcUaEndpointsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged("Name"); }
        }

        private string _description;

        public string Description
        {
            get { return _description; }
            set { _description = value; OnPropertyChanged("Description"); }
        }

        private int _endPoint;

        public int Endpoint
        {
            get { return _endPoint; }
            set { _endPoint = value; OnPropertyChanged("Endpoint"); }
        }

        private bool _isSelected;

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


来源:https://stackoverflow.com/questions/8212340/add-a-checkbox-in-wpf-datagrid-header-and-use-it-to-select-unselect-all-checkbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!