简体   繁体   English

如何使用 WPF Datagrid 复选框制作事件处理程序?

[英]How to make event handler with WPF Datagrid checkbox?

<DataGrid x:Name="MappingDataGrid">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="Included" Binding="{Binding Path=IsIncluded}"></DataGridCheckBoxColumn>
        <DataGridTextColumn Header="From" Binding="{Binding Path=KeyString}"></DataGridTextColumn>
        <DataGridTextColumn Header="To" Binding="{Binding Path=ValueString}"></DataGridTextColumn>
    </DataGrid.Columns>

I want to make Datagrid with Checkbox, the binding data is on MainWindow's member and I want to use a checkbox when I checked the box the binded object be changed.我想用复选框制作 Datagrid,绑定数据在 MainWindow 的成员上,当我选中更改绑定对象的框时,我想使用一个复选框。 However, I tried to find an event and event handler but I cannot find that.但是,我试图找到一个事件和事件处理程序,但我找不到。

you need to add event handler in the ViewModel您需要在 ViewModel 中添加事件处理程序

example:例子:

  public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

and add the event to the member in the ViewModel并将事件添加到 ViewModel 中的成员

        private string isIncluded;
    public string IsIncluded
    {
        get { return isIncluded; }
        set
        {
            isIncluded = value;
            OnPropertyChanged("IsIncluded");
        }
    }

and then in the xmal.cs you need to contact the Binding to the ViewModel然后在 xmal.cs 中,您需要联系绑定到 ViewModel

public partial class Mapping: Window
{
    private readonly ViewModel viewModel = new ViewModel();
    public ViewModel ()
    {
        InitializeComponent();
        DataContext = viewModel ;
    }
}

and add in the xmal after the binding the event并在绑定事件后添加 xmal

<DataGrid x:Name="MappingDataGrid">
<DataGrid.Columns>
    <DataGridCheckBoxColumn Header="Included" Binding="{Binding Path=IsIncluded,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></DataGridCheckBoxColumn>
    <DataGridTextColumn Header="From" Binding="{Binding Path=KeyString}"></DataGridTextColumn>
    <DataGridTextColumn Header="To" Binding="{Binding Path=ValueString}"></DataGridTextColumn>
</DataGrid.Columns>

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

相关问题 附加活动?如何在运行时删除WPF DataGrid的ScrollChanged事件处理程序 - Attached Event? How to remove ScrollChanged event handler for WPF DataGrid at runtime WPF DataGrid:如何区分对事件处理程序dataGrid_CurrentCellChanged的两次调用? - WPF DataGrid: How to discriminate between the 2 calls to event handler dataGrid_CurrentCellChanged? 数据网格中的复选框选中事件不会触发 wpf mvvm - checkbox in datagrid checked event not trigger wpf mvvm WPF CheckBox在滚动思想数据网格上检查事件 - WPF CheckBox checked event on scrolling thought datagrid 如何通过WPF DataGrid中的DataGrid Header CheckBox选择列的所有CheckBox - How to Select All CheckBox of a Column by DataGrid Header CheckBox in WPF DataGrid 如何选择WPF DataGrid中DataGrid Header CheckBox上列的所有CheckBox - How to Select All CheckBox of a Column on DataGrid Header CheckBox in WPF DataGrid 我如何在后面的代码中获取WPF DataGrid的Row CheckBox的Checked事件 - how can I get the WPF DataGrid's Row CheckBox's Checked Event in code behind 如何将Datagrid列的可见性绑定到WPF中的复选框 - How to bind the visibility of a Datagrid Column to a Checkbox in WPF 如何将事件处理程序绑定/添加到 Datagrid MVVM? - How to bind/add event handler to Datagrid MVVM? WPF Checkbox.Checked事件在加载数据网格之前触发 - WPF Checkbox.Checked event is firing before datagrid is loaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM