none
如何获取gridview的里面的分组的header? RRS feed

  • 问题

  • 我在项目里面用了一个gridview,其中header设置如下:

    <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <Grid x:Name="HeaderGrid" Height="68" Margin="0" HorizontalAlignment="Stretch">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock x:Name="TbGroupTitle" Margin="20,0,0,0" Grid.Column="0" FontWeight="SemiLight" VerticalAlignment="Center" Foreground="Black"   Width="{Binding Key, Converter={StaticResource SetWidth}}"
                                                   FontSize="30" Text="{Binding Key, Converter={StaticResource IcGroupToName}}"></TextBlock>
                                        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Column="1" Margin="50,0,50,0">
                                            <Image x:Name="AddNewItem" Source="Assets/ico_add_normal.png"
                                                   HorizontalAlignment="Right" Tapped="AddNewItem_OnTapped"></Image>
                                        </StackPanel>
                                    </Grid>
                                </DataTemplate>
    </GroupStyle.HeaderTemplate> 现在我有个问题就是想操作里面的textblock,我不知道怎么能得到这个header对象,请问怎么才能得到呢?

    2012年12月25日 5:14

答案

  • 你可以这样:

    void tableListView_Loaded(object sender, RoutedEventArgs e)
            {
                Grid obj = tableListView.HeaderTemplate.LoadContent() as Grid;
              
                foreach (TextBlock t in (obj as Grid).Children)
                {
                }

    你也可以遍历Grid的VisualTree:

    public T GetVisualChild<T>(DependencyObject parent) where T : DependencyObject
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                        child = GetVisualChild<T>(v);
                    if (child != null)
                        break;
                }
                return child;
            }

    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 BestRabbit 2012年12月28日 10:07
    2012年12月26日 6:13