如何判断 DataGrid 中的整行是否被选中,
或只有一列
(通过Binding
or Command
,我想保留 的原则MVVM
)。
最后,我自己找到了解决方案。
(很奇怪,我在搜索中一无所获。)
这是我所做的:
我将 DataGrid 的两个事件绑定到 ViewModel 中的命令。
<i:EventTrigger EventName="BeginningEdit">
<i:InvokeCommandAction CommandParameter="{Binding CurrentCell.Column.DisplayIndex, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Command="{Binding DataContext.ColBeginEdit, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</i:EventTrigger>
<i:EventTrigger EventName="CellEditEnding">
<i:InvokeCommandAction CommandParameter="{Binding CurrentCell.Column.Header, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Command="{Binding DataContext.ColEndEdit, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</i:EventTrigger>
在视图模型中,我对命令调用了两个函数:
public void CoulmnBeginEdit(object obj)
{
int i;
if (Int32.TryParse(obj.ToString(), out i))
{
if (i > 0)
this.CurrentCellEdit = i;
}
}
public void CoulmnEndEdit(object obj)
{
this.CurrentCellEdit = 0;
}
如果 CurrentCellEdit 大于 0 则表示行编辑,否则不。
我在 SelectedIndex 上的行号,根据 CurrentCellEdit 我也知道该行中的哪一列编辑。
我希望找到一种更简单的方法,但这是唯一想到的解决方案,以保持 MVVM 的原则,我仍在尝试..
您可以将属性 SelectionUnit 设置为“FullRow”