Toute idée sur la façon dont je peux obtenir ce qui suit dans un .Net 4 DataGrid:contenu cellulaire Supprimer dans un DataGrid WPF lorsque la touche Supprimer est pressée
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
}
}
}
Ce comportement devrait fonctionner avec une cellule, donc je don ne veulent pas coder en dur les noms de colonne ou de propriété.
EDIT: Solution Je suis venu avec:
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);
}
}
}