3

XamDataGrid を使用してデータを表示しています。ここで、各列に異なるコマンドを追加したいと思います。

グリッド全体で CellActivated イベントを使用してから ActiveCell にバインドすると、Viewmodel が View について認識し、Ac​​tiveCell によって返されたオブジェクトから Column を評価する方法を知る必要があるため、機能しません。

どのコマンドを呼び出す必要があるかを XamDataGrid に伝える方法を探しています。

私は次のようなものを想像します:

<igDP:Field Name="Dev"                  >
   <igDP:Field.Settings>
      <igDP:FieldSettings CellValuePresenterStyle="{StaticResource DevStyle}" ActivateCommand="{Binding DevCommand}/>
   </igDP:Field.Settings>
</igDP:Field>

コマンドがビューモデルまたはデータアイテムのプロパティである必要があるかどうかは気にしません。

これを実装するにはどうすればよいですか?

ありがとうございました

4

1 に答える 1

1

Attached BehaviorそしてMVVM手をつないで行きます。

添付された動作を介してイベントをViewmodel.ICommand処理し、イベントが処理されたときに実行されるように指定します。次に、処理されたイベントのイベント引数をViewModel.ICommandas コマンド パラメーターに送信できます。

あなたの添付プロパティ

 public static class MyBehaviors {

    public static readonly DependencyProperty CellActivatedCommandProperty
        = DependencyProperty.RegisterAttached(
            "CellActivatedCommand",
            typeof(ICommand),
            typeof(MyBehaviors),
            new PropertyMetadata(null, OnCellActivatedCommandChanged));

    public static ICommand CellActivatedCommand(DependencyObject o)
    {
        return (ICommand)o.GetValue(CellActivatedCommandProperty);
    }

    public static void SetCellActivatedCommand(
          DependencyObject o, ICommand value)
    {
        o.SetValue(CellActivatedCommandProperty, value);
    }

    private static void OnCellActivatedCommandChanged(
           DependencyObject d, 
           DependencyPropertyChangedEventArgs e)
    {
        var xamDataGrid = d as XamDataGrid;
        var command = e.NewValue as ICommand;
        if (xamDataGrid != null && command != null)
        {
           xamDataGrid.CellActivated +=
              (o, args) =>
                 {
                     command.Execute(args); 
                 };
        }
    }
}

あなたの XAML:

 <infragistics:XamDataGrid ...
        local:MyBehaviors.CellActivatedCommand="{Binding MyViewModelCommand}" />

それが役に立てば幸い。

于 2012-10-23T12:26:42.467 に答える