W procedurze obsługi zdarzenia dla polecenia DataGrid, otrzymuję DataGridCell w ExecutedRoutedEventArgs. Jednak nie mogłem dowiedzieć się, jak uzyskać powiązane DataGrid i DataGridRow. Twoja pomoc jest bardzo ceniona.Jak znaleźć właściciela DataGrid i DataGridRow z DataGridCell w WPF?
14
A
Odpowiedz
12
Prawdopodobnie chcą ustawić jakiegoś RelativeSource
wiązania, które mogą Ci się „siatkę rodzic/wiersz” poprzez {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}
, ale Twoje pytanie dało mi do myślenia ...
Mogłabyś:
użyć refleksji:
var gridCell = ....;
var parentRow = gridCell
.GetType()
.GetProperty("RowOwner",
BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(null) as DataGridRow;
Użyj VisualTreeHelper
:
var gridCell = ...;
var parent = VisualTreeHelper.GetParent(gridCell);
while(parent != null && parent.GetType() != typeof(DataGridRow))
{
parent = VisualTreeHelper.GetParent(parent);
}
0
Jednym ze sposobów można zrobić to poprzez przepuszczenie jednego lub obu potrzebnych elementów w postaci CommandParameter:
<MouseBinding
MouseAction="LeftDoubleClick"
Command="cmd:CustomCommands.Open"
CommandParameter="{Binding ElementName=MyDataGrid}}" />
Jeśli potrzebujesz obu, można dodać konwertera wielu wartości, że łączy je w Tuple
(lub pozostawić go jako obiekt [])
Następnie w kodzie opóźnieniem masz do niego dostęp za pomocą e.Parameter
2
Oto co myślę jest kompletnym rozwiązaniem ...
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;
}
Powiązane problemy
- 1. Uzyskiwanie datagridrow z widoku danych WPF
- 2. Jak wyodrębnić DataGridCell z klasy DataGridCellinfo w wpf
- 3. WPF DataGrid - Jak automatycznie wyjść z trybu edycji?
- 4. Podstawy WPF DataGrid AlternatingRowBackground i RowStyle
- 5. IsSelected Wiązanie w WPF DataGrid
- 6. Tworzenie obiektu ControlTemplate dla obiektu DataGridRow w pakiecie WPF
- 7. Zmiana komórkę w wierszu DataGrid (WPF) zmienia się komórek w wierszach poniżej
- 8. Binding właściwość DataGridCell ToolTip do wartości DataGridCell
- 9. WPF: Kopiowanie z DataGrid
- 10. WPF Uzyskaj UserControl właściciela
- 11. Jak odświeżyć datagrid w WPF
- 12. WPF DataGrid stylu Silverlight DataGrid?
- 13. Datagrid WPF: konwerter i StringFormat
- 14. Konwertuj i używaj DataTable w WPF DataGrid?
- 15. Edytowalny Datagrid w WPF
- 16. Wstępne sortowanie DataGrid w WPF
- 17. WPF DataGrid Trigger na zawartość komórki
- 18. opcje sortowania i filtrowania w WPF Datagrid?
- 19. Jak autoprzewijanie na datagrid WPF
- 20. Data formatowanie w WPF DataGrid
- 21. WPF ToolKit DataGrid Wydajność
- 22. Wyczyść wartości datagrid w wpf
- 23. Jak powiązać bool w tablicy 2D [] [] z danymi WPF DataGrid (w jedną stronę)?
- 24. WPF DataGrid wklejając
- 25. WPF DataGrid - Tworzenie nowej niestandardowej Kolumna
- 26. Scal komórki w WPF DataGrid
- 27. Jak zaimplementować edytowalne DataGridComboBoxColumn w WPF DataGrid
- 28. WPF Datagrid - automatyczne odświeżanie
- 29. Problem Wpf DataGrid
- 30. WPF DataGrid RowStyle dla wybranego wiersza bez zmiany koloru tła i pierwszego planu