【问题标题】:How to get the a control inside a Pivot.Itemtemplate on selection changed in UWP?如何在 UWP 中更改选择时获取 Pivot.Itemtemplate 内的控件?
【发布时间】:2016-05-06 09:46:39
【问题描述】:

我有以下代码:

<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> 的 ItemTemplate,我该如何解决这个问题?

编辑:此外,当我尝试使用 mainContentPivot.SelectedItem 时,我没有得到 FrameworkElement,因此我可以派生它的子级,我得到了 KeyValuePair&lt;CustomClass,List&lt;PivotItemMembers&gt;&gt; 。因此我无法使用 VisualTree。

【问题讨论】:

  • 使用 VisualTreeHelper GetChild 方法
  • 好吧,当我尝试使用 mainContentPivot.SelectedItem 时,我没有得到可以从中派生该元素的 xaml 元素。我得到了我的 KeyValuePair.
  • 检查我的答案。我不确定你在发件人中得到了什么。是 Pivot 还是 PivotItem?
  • 好的。检查更新的答案,让我知道它是否有效。因为我不能测试。您必须提供所有代码

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


【解决方案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;
            }

【讨论】:

  • 遗憾的是,我在发件人中获得了 Pivot 而不是 PivotItem。
  • 您可以使用ContainerFromItem 方法。您将获得 PivotItem。传递 selectedItem 以获得选中的 PivotItem。
  • 感谢我得到了对 PivotItem 的引用,但 VisualTree 正在为 GridView 返回 null 值。这是因为它被包裹在网格内吗?
  • 没有。它的递归函数。最终它应该返回网格的子网格,即 GridView。您能否提供简化的项目代码以便我可以尝试
  • 我无法获得 PivotItem 参考。它为我返回 null,使用您提供的代码
猜你喜欢
  • 2016-12-14
  • 2010-09-20
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
相关资源
最近更新 更多