问题
I have got the TabViewItem using:
var tab = PlaylistTabView.ContainerFromIndex(PlaylistTabView.SelectedIndex);
However, it doesn't seem that I am accessing the correct children.
This is the xaml of TabView.ItemTemplate
, where PlaylistControl
is my custom control and basically just a ListView
:
<controls:TabView.ItemTemplate>
<DataTemplate x:DataType="data:Playlist">
<local:PlaylistControl
AllowReorder="False"
AlternatingRowColor="True"
ItemsSource="{x:Bind Songs, Mode=OneWay}">
<local:PlaylistControl.Header>
<controls:ScrollHeader Mode="Sticky">
<Grid
x:Name="PlaylistInfoGrid"
Padding="30"
Background="#222222"
RequestedTheme="Dark">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image
x:Name="PlaylistCover"
Grid.RowSpan="4"
Width="180"
Height="180"
Margin="0,0,20,0"
Source="Assets/monotone_bg_wide.png" />
<TextBlock
Grid.Column="1"
FontSize="36"
Style="{StaticResource HeaderTextBlockStyle}"
Text="{x:Bind Name, Mode=OneWay}" />
<TextBlock
Grid.Row="1"
Grid.Column="1"
Margin="0,5"
Text="{x:Bind Songs, Converter={StaticResource SongCountConverter}, Mode=OneWay}" />
<CommandBar
Grid.Row="2"
Grid.Column="1"
Background="Transparent"
DefaultLabelPosition="Right"
Style="{StaticResource PlaylistCommandBarStyle}">
<AppBarButton
Click="Shuffle_Click"
Icon="Shuffle"
IsEnabled="{x:Bind Songs, Converter={StaticResource EnabledConverter}, Mode=OneWay}"
Label="Shuffle"
Style="{StaticResource PlaylistAppBarButtonStyle}"
ToolTipService.ToolTip="Shuffle Playlist" />
<AppBarButton
Click="AddTo_Click"
Icon="Add"
IsEnabled="{x:Bind Songs, Converter={StaticResource EnabledConverter}, Mode=OneWay}"
Label="Add To"
Style="{StaticResource PlaylistAppBarButtonStyle}"
ToolTipService.ToolTip="Add To Playlist" />
<AppBarButton
Click="Rename_Click"
Icon="Edit"
Label="Rename"
Style="{StaticResource PlaylistAppBarButtonStyle}"
ToolTipService.ToolTip="Rename Playlist" />
<AppBarButton
Click="Delete_Click"
Icon="Delete"
Label="Delete"
Style="{StaticResource PlaylistAppBarButtonStyle}"
ToolTipService.ToolTip="Delete Playlist" />
<AppBarButton
Click="More_Click"
Icon="More"
Label="More"
Style="{StaticResource PlaylistAppBarButtonStyle}"
ToolTipService.ToolTip="More Options" />
<CommandBar.SecondaryCommands>
<AppBarButton
HorizontalAlignment="Stretch"
Icon="Pin"
Label="Pin to Start"
ToolTipService.ToolTip="Pin Playlist to the Start Menu" />
<AppBarSeparator />
<AppBarToggleButton Label="Sort By Name" />
<AppBarToggleButton Label="Sort By Artist" />
<AppBarToggleButton Label="Sort By Album" />
<AppBarToggleButton Label="Sort By Duration" />
<AppBarToggleButton Label="Sort By Play Count" />
</CommandBar.SecondaryCommands>
</CommandBar>
</Grid>
</controls:ScrollHeader>
</local:PlaylistControl.Header>
</local:PlaylistControl>
</DataTemplate>
</controls:TabView.ItemTemplate>
I am trying to access the children of tab
using:
var i1 = VisualTreeHelper.GetChild(tab, 0);
var i2 = VisualTreeHelper.GetChild(i1, 0);
However, i1
is Grid
and i2
is Rectangle
. That's not what I expect. How can I get to the PlaylistControl.Header
?
回答1:
I have found the solution.
Since the ItemTemplate
will be loaded only once, the rest of the time I guess it is just refreshing the data bound to the control, I can register the Loaded
event of the controls that I need.
And in that Loaded
event, I can get the control I want from the parameter sender
, and save it as a class variable. Then I can do whatever I want.
来源:https://stackoverflow.com/questions/57830417/uwp-get-children-from-templated-tabviewitem