2012-04-25 54 views
0

我有一个ItemsControl,其中定义了一个GroupStyle以便按照一定的规则对我的项目进行分组。从代码隐藏中访问GroupStyle的DataTemplate控件

我想从我的代码隐藏中访问GroupStyle的DataTemplate中的项目。这样的事情可能吗?

这是GroupStyle的定义;我想在它访问无论是StackPanel的或不同的标签:

<ItemsControl ItemsSource="{Binding Conversations}" ItemTemplate="{DynamicResource ConversationTemplate}" Margin="15,1,15,1" x:Name="MainItemsControl"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel Orientation="Horizontal"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.HeaderTemplate> 
       <DataTemplate> 
        <Grid Margin="10, 20, -250, 8"> 
         <Grid.RowDefinitions> 
          <RowDefinition/> 
          <RowDefinition/> 
         </Grid.RowDefinitions> 
         <StackPanel x:Name="GroupStackPanel" Orientation="Horizontal" Grid.Row="0" Loaded="OnConversationGroupCreated" Tag="{Binding Name}"> 
          <Label x:Name="VolumeLabel" Style="{DynamicResource HighlightedMetadataStyle}" Content="{Binding Name.Volume}" Panel.ZIndex="3"/> 
          <Label x:Name="ActLabel" Style="{DynamicResource HighlightedMetadataStyle}" Margin="-16,0,0,0" Content="{Binding Name.Act}" Panel.ZIndex="2"/> 
          <Label x:Name="ChapterLabel" Style="{DynamicResource HighlightedMetadataStyle}" Margin="-16,0,0,0" Content="{Binding Name.Chapter}" Panel.ZIndex="1"/> 
         </StackPanel> 
         <Rectangle Height="1" Fill="#FFDDDDDD" Grid.Row="1" Margin="0,8,0,0"/> 
         <Rectangle Height="1" Fill="#FFEAEAEA" Grid.Row="1" Margin="0,9,0,0"/> 
        </Grid> 
       </DataTemplate> 
      </GroupStyle.HeaderTemplate> 
     </GroupStyle> 
    </ItemsControl.GroupStyle> 
</ItemsControl> 

回答

1

获得通过MainItemsControl.GroupStyle.HeaderTemplate 模板然后用VisualTreeHelper找到模板中正确的控制

相关问题