2011-07-27 30 views
1

DataGridにコントロール(コンボボックス、コードビハインドでSelectionChangedイベントを持つ)を持っている場合。 したがって、_SelectionChanged-eventから、グリッドのコンテナセルの参照を取得できますか? Plzヘルプ!セル参照を取得する方法

<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

あなたが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を忘れてはいけない - おそらくあなたは、あなたのを解決することができますスタイルの問題!

関連する問題