如何在UWP中更改选择中的Pivot.Itemtemplate中的控件?

时间:2016-05-06 09:46:39

标签: c# xaml win-universal-app winrt-xaml

我有以下代码:

<Pivot x:Name="mainContentPivot" 
           Margin="4,10,4,4"
           Style="{StaticResource pivotWithLargerArrows}" 
           SelectionChanged="mainContentPivot_SelectionChanged"
           ItemsSource="{Binding Path=Params}"
           >
        <Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Key}"/>
            </DataTemplate>
        </Pivot.HeaderTemplate>
        <Pivot.ItemTemplate>
            <DataTemplate>
                <Grid DataContext="{Binding Path=Value}">
                    <GridView x:Name="itemGV">
                             <GridView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding}" />
                                </DataTemplate>
                             </GridView.ItemTemplate> 
                    </GridView>
                </Grid>
            </DataTemplate>
    </Pivot.ItemTemplate>
</Pivot>

C#结束:

Dictionary<string,string> contentItems = new Dictionary<string,string>(){
    {"PivotItem1","ContentItem1"},
    {"PivotItem2","ContentItem2"},
    {"PivotItem3","ContentItem3"},
    {"PivotItem4","ContentItem4"},
    {"PivotItem5","ContentItem5"},
    {"PivotItem6","ContentItem6"},
}

 mainContentPivot.ItemsSource = contentItems;

在Pivot_SelectionChanged上,我想获得该枢轴中当前GridView的控件,以便我可以设置其ItemsSource。 但我无法做到这一点,因为Pivot绑定了一个'Dictionary&gt;的ItemTemplate,我如何解决这个问题?

编辑:当我尝试使用mainContentPivot.SelectedItem时,我没有获得FrameworkElement,因此我可以派生它的子项,我得到KeyValuePair<CustomClass,List<PivotItemMembers>>。因此我无法使用VisualTree。

1 个答案:

答案 0 :(得分:1)

编辑ItemContainerStyle并将ContentPresenter替换为您的项目模板

  <Style x:Key="PivotItemStyle1" TargetType="PivotItem">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Margin" Value="{ThemeResource PivotItemMargin}"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="PivotItem">
                        <Grid Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="Pivot">
                                    <VisualState x:Name="Right"/>
                                    <VisualState x:Name="Left"/>
                                    <VisualState x:Name="Center"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <!--<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>-->
                            <Grid >
                                <GridView x:Name="itemGV" ItemsSource="{Binding Path=Value}">
                                    <GridView.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding}" />
                                        </DataTemplate>
                                    </GridView.ItemTemplate>
                                </GridView>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

<Pivot x:Name="mainContentPivot" 
           Margin="4,10,4,4"
           SelectionChanged="mainContentPivot_SelectionChanged" ItemContainerStyle="{StaticResource PivotItemStyle1}"

           >
            <Pivot.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Key}"/>
                </DataTemplate>
            </Pivot.HeaderTemplate>
            <Pivot.ItemTemplate>
                <DataTemplate>
                    <Grid/>
                </DataTemplate>
            </Pivot.ItemTemplate>
        </Pivot>



  private void mainContentPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        PivotItem item = (sender as Pivot).ContainerFromItem((sender as Pivot).SelectedItem) as PivotItem;
        var gridView =    FindElementInVisualTree<GridView>(item);
        }

    private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
            {
                var count = VisualTreeHelper.GetChildrenCount(parentElement);
                if (count == 0) return null;

                for (int i = 0; i < count; i++)
                {
                    var child = VisualTreeHelper.GetChild(parentElement, i);
                    if (child != null && child is T)
                        return (T)child;
                    else
                    {
                        var result = FindElementInVisualTree<T>(child);
                        if (result != null)
                            return result;
                    }
                }
                return null;
            }