XAML
<DataGrid ItemsSource="{Binding Products}" CanUserAddRows="False" AutoGenerateColumns="False" SelectedItem="{Binding SelectedProduct}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnding">
<cmd:EventToCommand Command="{Binding ProdcutCellEditCmd,UpdateSourceTrigger=PropertyChanged}" PassEventArgsToCommand="True"></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding id}" Header="ID" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ParentLCSKU}" Header="LCSKU(Parent)" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ChildLCSKU}" Header="LCSKU(Child)" IsReadOnly="True"></DataGridTextColumn>
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ProductColors}}" SelectedValueBinding="{Binding Color}" Header="颜色" IsReadOnly="True"></DataGridComboBoxColumn>
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ProductSizes}}" SelectedValueBinding="{Binding Size}" Header="尺寸" IsReadOnly="True"></DataGridComboBoxColumn>
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ProductCategories}}" SelectedValueBinding="{Binding Category}" Header="类别" IsReadOnly="True"></DataGridComboBoxColumn>
<DataGridTextColumn Binding="{Binding Cost}" Header="成本"></DataGridTextColumn>
<DataGridCheckBoxColumn Binding="{Binding IsOEM,UpdateSourceTrigger=PropertyChanged}" Header="OEM"></DataGridCheckBoxColumn>
</DataGrid.Columns>
视图模型
public ProductVM()
{
ProdcutCellEditCmd = new RelayCommand<DataGridCellEditEndingEventArgs>(prodcutDataGridCellEditEnding);
}
public RelayCommand<DataGridCellEditEndingEventArgs> ProdcutCellEditCmd { get; set; }
private void prodcutDataGridCellEditEnding(DataGridCellEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
var prodcut = e.Row.DataContext as BaseProduct;
SelectedProduct = prodcut;
productSave();
}
}
我想在选中或取消选中复选框后触发CellEditEnding事件,但此事件仅在单元格失去焦点时触发。
这有什么不对吗?
抱歉我的英语不好。
答案 0 :(得分:1)
这是正常行为导致事件名称为CellEditEnding
。这意味着在完成单元格编辑后会触发事件。
我建议你绑定Command
中的CheckBox
属性。
如果DataGrid
置于Window
,您应该写:
<CheckBox Header="OEM" IsChecked="{Binding IsOEM,UpdateSourceTrigger=PropertyChanged}}"
Command="{Binding DataContext.ProdcutCellEditCmd, RelativeSource=
{RelativeSource AncestorType=Window, Mode=FindAncestor}}"}" />
如果DataGrid
放在UserControl
,您应该写:
<CheckBox Header="OEM" IsChecked="{Binding IsOEM,UpdateSourceTrigger=PropertyChanged}}"
Command="{Binding DataContext.ProdcutCellEditCmd, RelativeSource=
{RelativeSource AncestorType=UserControl, Mode=FindAncestor}}"}" />
答案 1 :(得分:1)
在这种情况下你可以做一件事,你必须更新像
这样的代码首先给出数据网格的名称,如
document.onkeydown = function(e) {
if (e.ctrlKey || e.shiftKey) {
return false;
} else {
return true;
}
};
现在您必须绑定Checkbox检查事件并传递datagrid行,以便您可以在ViewModel中访问该行的所有值
<DataGrid ItemsSource="{Binding Products}" Name="dg" CanUserAddRows="False" AutoGenerateColumns="False" SelectedItem="{Binding SelectedProduct}">
您的<DataGridCheckBoxColumn Binding="{Binding IsOEM,UpdateSourceTrigger=PropertyChanged}" Header="OEM">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding CheckBoxChecked,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=DataGrid}}" CommandParameter="{Binding ElementName=dg,Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGridCheckBoxColumn>
应该是这样的
我假设你已经在数据网格中绑定了List,所以基于它你的relay命令应该是
RelayCommand