[英]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.