0

私のプログラムでは、コンボボックスで項目が選択されるとすぐに、コンボボックスに (ViewModel から) バインドされたオブジェクトが更新されるという要件があります。現在、オブジェクトは、Enter キーを押すか、セルを離れることによって編集がコミットされた場合にのみ更新されます。ユーザーは余分なステップを望んでいません。

私の考えでは、コンボボックス内の項目を選択する行為で CommitEdit() メソッドをトリガーし、次に CancelEdit() をトリガーすることです。ただし、使用できないため、DataGridComboBoxColumn の SelectionChanged イベントにフックする方法を見つけることができないようです。

その他の提案は、ビューモデルでプロパティ変更イベントをリッスンすることですが、セル編集が完了するまでプロパティは変更されません。

ユーザーが Enter キーを押したか、セルを離れたかのように、DataGridCombobox で新しい項目 (インデックス) を選択してセルの編集を閉じる方法を考えられる人はいますか?

注: お客様の制限により、.NET 4.5 を使用できません。

4

1 に答える 1

1

同様の問題がありましたが、添付プロパティを使用して解決策を見つけました。これは問題を正確に解決しない可能性がありますが、データグリッドの選択が変更された問題に役立ちます。

以下は、添付されたプロパティとハンドラー メソッドです。

public static readonly DependencyProperty ComboBoxSelectionChangedProperty = DependencyProperty.RegisterAttached("ComboBoxSelectionChangedCommand",
                                                                                                              typeof(ICommand),
                                                                                                              typeof(SpDataGrid),
                                                                                                              new PropertyMetadata(new PropertyChangedCallback(AttachOrRemoveDataGridEvent)));


public static void AttachOrRemoveDataGridEvent(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
  DataGrid dataGrid = obj as DataGrid;
  if (dataGrid != null)
  {   
      if (args.Property == ComboBoxSelectionChangedProperty)
      {
        dataGrid.SelectionChanged += OnComboBoxSelectionChanged;
      }
    }
    else if (args.OldValue != null && args.NewValue == null)
    { if (args.Property == ComboBoxSelectionChangedProperty)
      {
        dataGrid.SelectionChanged -= OnComboBoxSelectionChanged;
      }
}
}

private static void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs args)
{
  DependencyObject obj = sender as DependencyObject;
  ICommand cmd = (ICommand)obj.GetValue(ComboBoxSelectionChangedProperty);
  DataGrid grid = sender as DataGrid;

  if (args.OriginalSource is ComboBox)
  {
    if (grid.CurrentCell.Item != DependencyProperty.UnsetValue)
    {
      //grid.CommitEdit(DataGridEditingUnit.Row, true);
      ExecuteCommand(cmd, grid.CurrentCell.Item);
    }
  }
}

SpDataGrid は、データ グリッドから継承したカスタム コントロールです。

スタイルに resourcedictionary を使用するため、generic.xaml に以下のスタイルを追加しました (確かにデータグリッド内に追加できます)。

<Style TargetType="{x:Type Custom:SpDataGrid}">
     <Setter Property="Custom:SpDataGrid.ComboBoxSelectionChangedCommand" Value="{Binding ComboBoxSelectionChanged}"/>
 </Style>

ComboBoxSelectionChanged は、ビューモデルのコマンドです。OnComboBoxSelectionChanged 私の場合、値は既に更新されているため、commitedit にコメントしました。

不明な点や質問があればお知らせください。お役に立てれば。

于 2013-01-24T20:10:14.967 に答える