2012-09-28 67 views
1

我正在使用WPF LOB應用程序並使用Prism和委託命令將UI與View Model分開。如何在WPF MVVM應用程序中傳遞DataGridCellEditEventArgs

當用戶對特定的單元格從UI(而不是從視圖模型或服務)進行更改時,我需要調用一些其他功能。

我已經創建了附加的行爲

public static class DataGridCellEditEndingBehaviour 
{ 
    private static readonly DependencyProperty CellEditEndingProperty 
     = DependencyProperty.RegisterAttached(
     "CellEditEnding", 
     typeof(CellEditEnding), 
     typeof(DataGridCellEditEndingBehaviour), 
     null); 

    public static readonly DependencyProperty CommandProperty 
     = DependencyProperty.RegisterAttached(
     "Command", 
     typeof(ICommand), 
     typeof(DataGridCellEditEndingBehaviour), 
     new PropertyMetadata(OnSetCommandCallback)); 

    public static readonly DependencyProperty CommandParameterProperty 
     = DependencyProperty.RegisterAttached(
     "CommandParameter", 
     typeof(object), 
     typeof(DataGridCellEditEndingBehaviour), 
     new PropertyMetadata(OnSetCommandParameterCallback)); 

    public static ICommand GetCommand(DataGrid control) 
    { 
     return control.GetValue(CommandProperty) as ICommand; 
    } 

    public static void SetCommand(DataGrid control, ICommand command) 
    { 
     control.SetValue(CommandProperty, command); 
    } 

    public static void SetCommandParameter(DataGrid control, object parameter) 
    { 
     control.SetValue(CommandParameterProperty, parameter); 
    } 

    public static object GetCommandParameter(DataGrid control) 
    { 
     return control.GetValue(CommandParameterProperty); 
    } 

    private static void OnSetCommandCallback 
     (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
    { 
     DataGrid control = dependencyObject as DataGrid; 
     if (control != null) 
     { 
      CellEditEnding behavior = GetOrCreateBehavior(control); 
      behavior.Command = e.NewValue as ICommand; 
     } 
    } 

    private static void OnSetCommandParameterCallback 
     (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
    { 
     DataGrid control = dependencyObject as DataGrid; 
     if (control != null) 
     { 
      CellEditEnding behavior = GetOrCreateBehavior(control); 
      behavior.CommandParameter = e.NewValue; 
     } 
    } 

    private static CellEditEnding GetOrCreateBehavior(DataGrid control) 
    { 
     CellEditEnding behavior = 
      control.GetValue(CellEditEndingProperty) as CellEditEnding; 
     if (behavior == null) 
     { 
      behavior = new CellEditEnding(control); 
      control.SetValue(CellEditEndingProperty, behavior); 
     } 
     return behavior; 
    } 
} 

public class CellEditEnding : CommandBehaviorBase<DataGrid> 
{ 
    public CellEditEnding(DataGrid control) 
     : base(control) 
    { 
     control.CellEditEnding += OnCellEditEnding; 
    } 

    private void OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
    { 
     ExecuteCommand(); 
    } 
} 

,我能夠使用

local:DataGridCellEditEndingBehaviour.Command ="{Binding CellChangedCommand}" 
  1. 當被調用時,我沒有得到任何的EventArgs中調用相同我的代理命令在虛擬機,我如何檢索事件參數,我可以通過命令參數設置它?如果是這樣,我如何將事件參數傳遞給委託命令?

  2. 在CellEditEndigEvent期間,該值尚未存儲到VM中,因爲它仍處於轉換中,是否有辦法強制它從處理程序發生,所以我不需要從中讀取值CellEditEndingEventArgs,而是我可以直接從VM讀取?

回答

0

這是一個附加屬性。您可以使用它像 地方:DataGridCellEditEndingBehaviour.CommandParameter =「{結合對任何你想通過}」

你可能已經實現其具有指示已編輯單元格或東西沿線自定義屬性自定義數據網格。

0

我遇到了這個試圖解決類似的問題 - 在MVVM應用程序中,我們有一個帶有DataGrid的UserControl,因此我們需要將RowEditEnding事件綁定到Command。我不能完全按照上面的例子,並且無法確定如何查找CommandBehaviorBase。

部分基於答案在MvvmLight EventToCommand and WPFToolkit DataGrid double-click,我實現了我們的AttachedBehaviour如下:

Public Class DataGridHelper 
Public Shared ReadOnly RowEditEndingCommandProperty As DependencyProperty = 
    DependencyProperty.RegisterAttached("RowEditEndingCommand", 
             GetType(ICommand), 
             GetType(DataGridHelper), 
             New UIPropertyMetadata(AddressOf OnRowEditEnding)) 

Public Shared Sub SetRowEditEndingCommand(control As DataGrid, command As ICommand) 
    control.SetValue(RowEditEndingCommandProperty, command) 
End Sub 

Private Shared Sub OnRowEditEnding(dependencyObject As DependencyObject, e As DependencyPropertyChangedEventArgs) 
    Dim control As DataGrid = TryCast(dependencyObject, DataGrid) 
    If control Is Nothing Then 
     Throw New InvalidOperationException("This behavior can be attached to a DataGrid item only.") 
    End If 

    If e.NewValue IsNot Nothing AndAlso e.OldValue Is Nothing Then 
     AddHandler control.RowEditEnding, AddressOf RowEditEnding 
    ElseIf e.NewValue Is Nothing AndAlso e.OldValue IsNot Nothing Then 
     RemoveHandler control.RowEditEnding, AddressOf RowEditEnding 
    End If 
End Sub 

Private Shared Sub RowEditEnding(sender As Object, e As DataGridRowEditEndingEventArgs) 
    Dim element As UIElement = DirectCast(sender, UIElement) 
    Dim command As ICommand = DirectCast(element.GetValue(DataGridHelper.RowEditEndingCommandProperty), ICommand) 
    'command.Execute(Nothing) 
    command.Execute(e) 
End Sub 
End Class 

到目前爲止,這似乎工作,並顯得比上述方法更簡單。我們將DataGridRowEditEndingEventArgs傳遞迴參數中的Command,所以它在ViewModel中可用。這可能也適用於CellEditEnding事件。

相關問題