2011-07-27 76 views
1

如果我在DataGrid中有一个控件(ComboBox,在代码隐藏中有一个SelectionChanged事件)。 因此,从_SelectionChanged事件,我可以得到它的网格的容器单元的引用? Plz Help !!如何获取单元格引用

<DataGridTemplateColumn Width="100"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Path=QuotationItemCode}"/> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 

    <DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 

       <ComboBox Height="22" Width="100" Name="cmbQuotationItemCode" 
          ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.vwProducts}" 
          DisplayMemberPath="itM_Code" 
          SelectedValuePath ="itM_Id" 
          Tag="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Row[2]}" 
          SelectedValue="{Binding Path=QuotationItemId}" 
          Text="{Binding Path=QuotationItemCode}" SelectionChanged="cmbQuotationItemCode_SelectionChanged"> 
       </ComboBox> 
       <TextBlock Name="txtQuotationItemDescription" Text="{Binding Path=DetailDescription, IsAsync=True}" Height="19"></TextBlock> 

      </StackPanel> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate> 

</DataGridTemplateColumn> 

回答

1

您可以步行从ComboBox的可视化树,直到你打一个DataGridCell,使用VisualTreeHelper这样的:

private static T FindAncestor<T>(DependencyObject child) where T : DependencyObject 
    { 
     var parentObject = VisualTreeHelper.GetParent(child); 

     if(parentObject == null || parentObject is T) { 
      return (T)parentObject; 
     } 

     return FindAncestor<T>(parentObject); 
    } 

    private void cmbQuotationItemCode_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var cell = FindAncestor<DataGridCell>((DependencyObject)sender); 
     ... 
    } 

这就是说,不要忘了DataGridTemplateColumn.CellStyle - 也许你可以解决你的风格问题!

相关问题