Reputation: 1967
I have the following code:
<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# End:
Dictionary<string,string> contentItems = new Dictionary<string,string>(){
{"PivotItem1","ContentItem1"},
{"PivotItem2","ContentItem2"},
{"PivotItem3","ContentItem3"},
{"PivotItem4","ContentItem4"},
{"PivotItem5","ContentItem5"},
{"PivotItem6","ContentItem6"},
}
mainContentPivot.ItemsSource = contentItems;
On the Pivot_SelectionChanged I want to get a control of the current GridView in that pivot so I can setup its ItemsSource. But I'm unable to do this as the Pivot is bound to an ItemTemplate of `Dictionary>, How do I solve this issue?
Edit: Also when I try using mainContentPivot.SelectedItem I don't get a FrameworkElement so that I can derive its children I get a KeyValuePair<CustomClass,List<PivotItemMembers>>
. Hence I'm not able to use the VisualTree.
Upvotes: 0
Views: 950
Reputation: 3221
Edit the ItemContainerStyle and replace ContentPresenter with your item template
<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;
}
Upvotes: 1