2012-11-22 64 views
2

我有一個DataGrid和DataGridTemplateColumns。在TemplateColumn中,我使用了一個DataTrigger,它工作正常。它從DataGrid父項中檢索項目計數。從xaml獲取RowIndex

<DataGridTemplateColumn>               
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      ... 
      <!-- this works fine! --> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
       AncestorType={x:Type DataGrid}}, Path=Items.Count}" Value="1"> 
        ... 
      </DataTrigger> 
      </DataTemplate> 

是否有可能檢索放置模板的當前RowIndex? 我認爲可以綁定到當前的DataGridRow。的綁定路徑「GetIndex()」將不支持,如:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
    AncestorType={x:Type DataGridRow}}, Path=GetIndex()}" Value="0"> <!-- error: GetIndex() --> 

是否有其他的綁定到DataGridRow.GetIndex()從XAML?

回答

3

您只能綁定到Properties而不是對象的方法。你需要在你希望綁定到方法的情況下使用IValueConverter -

public class MyConverter : DependencyObject, IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, 
          System.Globalization.CultureInfo culture) 
    { 
     return (value as DataGridRow).GetIndex(); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
           System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

,並將其綁定這樣的 -

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
         AncestorType={x:Type DataGridRow}}, 
         Converter={StaticResource MyConverter}}" 
      Value="0"> 
+0

OK,很好的解釋。 – deafjeff

相關問題