C# 如何使用WPF Datagrid复选框创建事件处理程序?

C# 如何使用WPF Datagrid复选框创建事件处理程序?,c#,wpf,C#,Wpf,我想使用复选框创建Datagrid,绑定数据位于MainWindow的成员上,并且我想在选中要更改绑定对象的复选框时使用复选框。但是,我试图找到一个事件和事件处理程序,但找不到 您需要在ViewModel中添加事件处理程序 public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string property) { if (Property

我想使用复选框创建Datagrid,绑定数据位于MainWindow的成员上,并且我想在选中要更改绑定对象的复选框时使用复选框。但是,我试图找到一个事件和事件处理程序,但找不到

您需要在ViewModel中添加事件处理程序

  public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
        private string isIncluded;
    public string IsIncluded
    {
        get { return isIncluded; }
        set
        {
            isIncluded = value;
            OnPropertyChanged("IsIncluded");
        }
    }
例如:

<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>
并将事件添加到ViewModel中的成员

  public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
        private string isIncluded;
    public string IsIncluded
    {
        get { return isIncluded; }
        set
        {
            isIncluded = value;
            OnPropertyChanged("IsIncluded");
        }
    }
然后在xmal.cs中,您需要联系ViewModel的绑定

  public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
        private string isIncluded;
    public string IsIncluded
    {
        get { return isIncluded; }
        set
        {
            isIncluded = value;
            OnPropertyChanged("IsIncluded");
        }
    }
并在绑定事件后添加xmal

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

您需要在ViewModel中添加事件处理程序

  public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
        private string isIncluded;
    public string IsIncluded
    {
        get { return isIncluded; }
        set
        {
            isIncluded = value;
            OnPropertyChanged("IsIncluded");
        }
    }
例如:

<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>
并将事件添加到ViewModel中的成员

  public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
        private string isIncluded;
    public string IsIncluded
    {
        get { return isIncluded; }
        set
        {
            isIncluded = value;
            OnPropertyChanged("IsIncluded");
        }
    }
然后在xmal.cs中,您需要联系ViewModel的绑定

  public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
        private string isIncluded;
    public string IsIncluded
    {
        get { return isIncluded; }
        set
        {
            isIncluded = value;
            OnPropertyChanged("IsIncluded");
        }
    }
并在绑定事件后添加xmal

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


它不是这样工作的。当您对DependencyProperty或实现INotifyPropertyChanged的对象使用绑定时,模型的更新会自动进行,并且您使用双向绑定,然后在更新模型时UI也会更新。因此,基本上您需要正确地实现绑定,并且它将在不使用显式“事件处理程序”的情况下工作,而显式“事件处理程序”不是它的工作方式。当您对DependencyProperty或实现INotifyPropertyChanged的对象使用绑定时,模型的更新会自动进行,并且您使用双向绑定,然后在更新模型时UI也会更新。因此,基本上您需要正确地实现绑定,并且它将在不使用显式“事件处理程序”的情况下工作