按下Delete键时删除WPF DataGrid中的单元格内容

时间:2010-12-10 17:27:10

标签: c# wpf datagrid

关于如何在.Net 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
        }
    }
}

此行为适用于任何单元格,因此我不想对列或属性名称进行硬编码。

编辑:解决方案我想出了:

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);
          }
    }
}

2 个答案:

答案 0 :(得分:3)

我需要做几件事才能让它发挥作用:

  1. 在编辑时过滤掉删除键按下(找不到明显的方法来检测是否 在编辑模式下):

    private bool _isEditing = false;
    private void datagrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {   _isEditing = true; }
    
    private void datagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {   _isEditing = false; }
    
  2. 处理KeyUp消息(Keygown消息由datagrid处理):

    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);
               }
            }
        }
    }
    
  3. 使用框架帮助程序类将value = null路由到基础视图模型

    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
    }
    

答案 1 :(得分:0)

这可能相当复杂,取决于单元格的模板等。

我想你必须使用各种BindingOperations方法(BindingOperations.GetBindingBindingOperations.GetBindingExpression等)来破坏绑定值?

相关问题