2010-10-06 10 views

risposta

12

Probabilmente desidera impostare una sorta di RelativeSource vincolante che si può ottenere la "griglia genitore/riga" attraverso un {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, ma la tua domanda mi ha fatto pensare ...

Si potrebbe:

utilizzare la riflessione:

var gridCell = ....; 
var parentRow = gridCell 
     .GetType() 
     .GetProperty("RowOwner", 
       BindingFlags.NonPublic | BindingFlags.Instance) 
     .GetValue(null) as DataGridRow; 

Utilizzare il VisualTreeHelper:

var gridCell = ...; 
var parent = VisualTreeHelper.GetParent(gridCell); 
while(parent != null && parent.GetType() != typeof(DataGridRow)) 
{ 
    parent = VisualTreeHelper.GetParent(parent); 
} 
0

Un modo che si possa fare è quello di passare uno o entrambi gli elementi necessari in un CommandParameter:

<MouseBinding 
    MouseAction="LeftDoubleClick" 
    Command="cmd:CustomCommands.Open" 
    CommandParameter="{Binding ElementName=MyDataGrid}}" /> 

Se avete bisogno di entrambi, si potrebbe aggiungere un convertitore multi-valore che li combina in un Tuple (o lasciarlo come un oggetto [])

Poi nel code-behind è possibile accedervi utilizzando e.Parameter

2

Ecco quello che penso è una risposta 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; 
    } 
Problemi correlati