简体   繁体   中英

How to get <checkbox> state inside <datagridtemplatecolumn> on any row's checkbox event in datagrid

I have a datagrid - dgUserSession in my WPF application. It contains 3 columns where only 2 text columns are bound with database and first one is checkbox created manually.

<DataGridTemplateColumn>
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox IsChecked="False" Name="userSelection" Checked="DataGridCheckBox_Checked" Unchecked="DataGridCheckBox_Unchecked"/>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                    <DataGridTemplateColumn.HeaderTemplate>
                                        <DataTemplate>
                                            <CheckBox Checked="chkHeader_Checked" Unchecked="chkHeader_Unchecked" Name="headerSelection">
                                            </CheckBox>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.HeaderTemplate>
                                </DataGridTemplateColumn>

I want to iterate through each rows state in all four events

private void chkHeader_Checked(object sender, RoutedEventArgs e)
    { 
        for (int i = 0; i < dgUserSession.Items.Count; i++)
        {
}
}

But how to get the checkbox is wether checked or not during this iteration? I spent 3hrs in goggle but no result.

Thanks in advance.

In WPF, it is always best to create your data type classes in the form that is required by the UI . By that, I mean that instead of data binding to two of the three columns in your DataGrid , you should be binding to all three columns. If you add a bool property into the class that is being displayed in each row of the DataGrid , then you can get access to the value of the Checkbox from that class.

If you have not data bound a collection of a custom type to your DataGrid.ItemsSource , then I'd recommend that you do that. Just create a class (that implements the INotifyPropertyChanged interface) and has a property for each column (3 in your case). Then data bind an ObservableCollection of that type to your DataGrid.ItemsSource property:

public ObservableCollection<YourDataType> Items { get; set; }

...

Then in the property setters of your YourDataType class, you will have access to the values of each property/column each time any value is changed:

public someType SomeProperty
{
    get { return someProperty; }
    set 
    {
        someProperty = value;
        NotifyPropertyChanged("SomeProperty");
        // each time this is changed, you have access to the other property values here
    }
}

You can also iterate through the collection to find the values of the Checkbox es:

foreach (YourDataType item in Items)
{
    bool checkBoxValue = item.NewBoolProperty;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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