私はネット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);
}
}
}