2010-12-10 12 views
1

私はネット4のDataGridに次のように達成できる方法上の任意のアイデアを押したとき:削除セルの内容の削除キーが

private void grid_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Key == Key.Delete) 
    { 
     DataGridCell cell = e.OriginalSource as DataGridCell; 

     if (cell == null) { return; } 

     if (!cell.IsReadOnly && cell.IsEnabled) 
     { 
      // Set the cell content (and the property of the object binded to it) 
      // to null 
     } 
    } 
} 

この動作は、Iドンので、任意のセルで動作するはずです列名またはプロパティ名をハードコーディングする必要はありません。

編集:ソリューション私が思いついた:などの細胞のテンプレートに応じて、

かなり複雑になる可能性が
if (e.Key == Key.Delete) 
{ 
    DataGridCell cell = e.OriginalSource as DataGridCell; 

    if (cell == null) { return; } 

    if (!cell.IsReadOnly && cell.IsEnabled) 
    { 
      TextBlock tb = cell.Content as TextBlock; 

      if (tb != null) 
      { 
       Binding binding = BindingOperations.GetBinding(tb, TextBlock.TextProperty); 

       if (binding == null) { return; } 

       BindingExpression exp = BindingOperations.GetBindingExpression(tb, TextBlock.TextProperty); 

       PropertyInfo info = exp.DataItem.GetType().GetProperty(binding.Path.Path); 

       if (info == null) { return; } 

       info.SetValue(exp.DataItem, null, null); 
      } 
    } 
} 

答えて

0

私はあなたが様々な使用する必要があるだろう想像しますBindingOperationsメソッド(BindingOperations.GetBinding、など)をバインドされた値で混乱させるのですか?

3

あり、私はこの作業を取得するためにしなければならなかった物事のカップル:

  1. (編集モードで 場合を検出するための明確な方法を見つけることができませんでした)編集するときにフィルターが出キーの押下を削除します。

    private void dataGrid_KeyUp(object sender, KeyEventArgs e) 
    { 
        if (!_isEditing && e.Key == Key.Delete && Keyboard.Modifiers == ModifierKeys.None) 
        { 
         foreach (var cellInfo in dataGrid.SelectedCells) 
         { 
          var column = cellInfo.Column as DataGridBoundColumn; 
          if (column != null) 
          { 
           var binding = column.Binding as Binding; 
           if (binding != null) 
            BindingHelper.SetSource(cellInfo.Item, binding, null); 
          } 
         } 
        } 
    } 
    
  2. private bool _isEditing = false; 
    private void datagrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) 
    { _isEditing = true; } 
    
    private void datagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
    { _isEditing = false; } 
    
  3. (KeyDownイベントメッセージは、データグリッドによって処理された)keyUpイベントメッセージを処理

  4. public class BindingHelper: FrameworkElement 
    { 
        public static void SetSource(object source, Binding binding, object value) 
        { 
         var fe = new BindingHelper(); 
         var newBinding = new Binding(binding.Path.Path) 
         { 
          Mode = BindingMode.OneWayToSource, 
          Source = source, 
         }; 
         fe.SetBinding(ValueProperty, newBinding); 
         fe.Value = value; 
        } 
    
        #region Value Dependency Property 
        public object Value 
        { 
         get { return (object)GetValue(ValueProperty); } 
         set { SetValue(ValueProperty, value); } 
        } 
    
        public static readonly DependencyProperty ValueProperty = 
         DependencyProperty.Register("Value", typeof(object), typeof(BindingHelper)); 
        #endregion 
    } 
    
下層のビューモデルにヌル=ルートに値をフレームワークヘルパークラスを使用
関連する問題