1
我有一個DataGrid,用戶只能使用綁定到視圖模型的add命令輸入新行。下面顯示的附加行爲激活正確的單元格。DataGrid禁用行wpf
我現在要做的是有效地使新行'模態'。也就是說,我不希望用戶能夠對網格執行其他任何操作,直到新行有效並提交,或者編輯被取消。
假設我的視圖模型知道它何時有效並實現了IEditableObject,我可以從我的附加行爲中獲得所有這些嗎?必須做些什麼?
乾杯,
Berryl
代碼
public class NewItemAddedByCommandBehavior : Behavior<DataGrid>
{
private MainWindowViewModel _vm;
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.DataContextChanged += OnAssociatedObject_DataContextChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.DataContextChanged -= OnAssociatedObject_DataContextChanged;
_vm.NewItemAddedByCommand -= OnNewItemAddedByCommand;
}
private void OnAssociatedObject_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
_vm = (MainWindowViewModel) AssociatedObject.DataContext;
_vm.NewItemAddedByCommand += OnNewItemAddedByCommand;
}
private void OnNewItemAddedByCommand(object sender, EventArgs e)
{
var currentItem = _vm.SelectedItem;
var col = AssociatedObject.Columns[1];
AssociatedObject.CurrentCell = new DataGridCellInfo(currentItem, col);
AssociatedObject.ScrollIntoView(currentItem, col);
AssociatedObject.Focus();
AssociatedObject.BeginEdit();
}
}