En un controlador de eventos para un comando para un DataGrid, obtengo DataGridCell en ExecutedRoutedEventArgs. Sin embargo, no pude encontrar la forma de obtener su DataGrid y DataGridRow asociados. Su ayuda es muy apreciada.¿Cómo encontrar su propietario DataGrid y DataGridRow de DataGridCell en WPF?
14
A
Respuesta
12
Probablemente quiere establecer algún tipo de RelativeSource
unión que se puede obtener la "red de padres/fila" a través de un {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}
, pero tu pregunta me hizo pensar ...
Usted podría:
uso Reflexión:
var gridCell = ....;
var parentRow = gridCell
.GetType()
.GetProperty("RowOwner",
BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(null) as DataGridRow;
Utilice la VisualTreeHelper
:
var gridCell = ...;
var parent = VisualTreeHelper.GetParent(gridCell);
while(parent != null && parent.GetType() != typeof(DataGridRow))
{
parent = VisualTreeHelper.GetParent(parent);
}
0
Una forma que podría hacer es pasando uno o ambos de los elementos necesarios como CommandParameter:
<MouseBinding
MouseAction="LeftDoubleClick"
Command="cmd:CustomCommands.Open"
CommandParameter="{Binding ElementName=MyDataGrid}}" />
Si necesita tanto, se podría añadir un convertidor de valores múltiples que las combina en una Tuple
(o dejarlo como un objeto [])
Luego, en el código subyacente puede acceder a él mediante el uso de e.Parameter
2
Esto es lo que creo que es una respuesta completa ...
private void Copy(object sender, ExecutedRoutedEventArgs e)
{
DataGrid grid = GetParent<DataGrid>(e.OriginalSource as DependencyObject);
DataGridRow row = GetParent<DataGridRow>(e.OriginalSource as DependencyObject);
}
private T GetParent<T>(DependencyObject d) where T:class
{
while (d != null && !(d is T))
{
d = VisualTreeHelper.GetParent(d);
}
return d as T;
}
Cuestiones relacionadas
- 1. WPF DataGrid MouseOver en DataGridRow
- 2. ¿Cómo hacer WPF DataGridCell ReadOnly?
- 3. Condicionalmente haciendo readonly a WPF DataGridCell
- 4. WPF DataGrid AutoSize Issue
- 5. WPF - ¿Cómo obtener una celda de un DataGridRow?
- 6. WPF DataGrid: cómo salir automáticamente del modo de edición?
- 7. Cómo extraer DataGridCell de la clase DataGridCellinfo en wpf
- 8. WPF arrastre y suelte en DataGrid
- 9. Estilo WPF DataGrid-Silverlight DataGrid?
- 10. WPF Datagrid establece la fila seleccionada
- 11. WPF datagrid: convertidor y StringFormat
- 12. Enlace dinámico en la plantilla WGT DataGridCell
- 13. WPF datagrid y la tecla de tabulación
- 14. ¿Convertir y usar DataTable en WPF DataGrid?
- 15. WPF datagrid con MVVM
- 16. Rendimiento lento con WPF DataGrid y ScrollViewer
- 17. Disparador de estilo WPF para DataGridRow Color de fondo superado por AlternatingRowBackground Brush
- 18. WPF DataGrid: ¿Reordenar filas?
- 19. WPF DataGrid RowHeader databinding
- 20. ¿Cómo puedo encontrar el PROPIETARIO de un objeto en Oracle?
- 21. WPF DataGrid Plantilla de control
- 22. WPF ToolKit DataGrid Performance
- 23. Cómo implementar DataGridComboBoxColumn editable en WPF DataGrid
- 24. Wpf DataGrid problema
- 25. Preordenamiento de un DataGrid en WPF
- 26. WPF Datagrid Performance
- 27. StringFormat personalizado en WPF DataGrid
- 28. WPF Toolkit Datagrid: ¿cómo desactivas la selección?
- 29. WPF DataGrid SelectedItem
- 30. WPF DataGrid pegar