2012-09-28 2 views
1

WPF LOB 응용 프로그램 및 프리즘 사용 및 뷰 모델에서 UI를 구분하는 명령을 위임하고 있습니다.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를하지 않는 사용하여 동일한을 호출 할 수 있어요 VM에서 내 delegateCommand, 어떻게 이벤트 인수를 검색 할 수 있습니까, 내가 명령 매개 변수를 통해 그것을 설정할 수 있습니까? 그렇다면 어떻게하면 위임 명령에 이벤트 인수를 전달할 수 있습니까?

  2. CellEditEndigEvent가 진행되는 동안 값이 아직 VM에 아직 저장되어 있지 않으므로 처리기에서 강제로 값을 읽을 수있는 방법이 있으므로 값을 읽을 필요가 없습니다. CellEditEndingEventArgs 대신 VM에서 직접 읽을 수 있습니까?

답변

0

첨부 된 속성입니다. DataGridCellEditEndingBehaviour.CommandParameter = 당신은 아마 라인을 따라 편집 된 셀 또는 무언가를 나타내는 사용자 정의 속성이 사용자 정의 데이터 그리드를 구현 한

을 "{당신이 전달하려는 어떤에 바인딩}": 당신은 지역처럼 사용합니다.

0

MVVM 응용 프로그램에서 DataGrid가있는 UserControl이 있으므로 비슷한 문제를 해결하기 위해이 문제가 발생했습니다. 따라서 RowEditEnding 이벤트를 명령에 바인딩해야합니다. 위의 예제를 따라갈 수 없었고 CommandBehaviorBase를 찾는 방법을 결정할 수 없었습니다.

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 

지금까지이 작동하는 것 같다, 위의 방법보다 간단 나타납니다 다음과 같이

부분적 MvvmLight EventToCommand and WPFToolkit DataGrid double-click의 답변에 따라, 나는 우리 AttachedBehaviour을 구현했습니다. DataGridRowEditEndingEventArgs를 매개 변수의 Command로 다시 전달하므로 ViewModel에서 사용할 수 있습니다. 이것은 아마도 CellEditEnding 이벤트에도 적용됩니다.

관련 문제