繁体   English   中英

从代码隐藏访问GroupStyle的DataTemplate控件

[英]Accessing GroupStyle's DataTemplate controls from code-behind

我有一个定义有GroupStyle的ItemsControl,以便根据某些规则对我的项目进行分组。

我试图从我的代码隐藏访问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>

通过MainItemsControl.GroupStyle.HeaderTemplate获取模板,然后使用VisualTreeHelper在模板内部找到正确的控件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM