2010-10-06 10 views

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