我有使用TabViewItem:
var tab = PlaylistTabView.ContainerFromIndex(PlaylistTabView.SelectedIndex);
然而,这似乎并不认为我访问正确的儿童。
这是的XAML TabView.ItemTemplate
,其中PlaylistControl
是我的自定义控制,基本上只是一个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>
我试图访问的孩子tab
使用:
var i1 = VisualTreeHelper.GetChild(tab, 0);
var i2 = VisualTreeHelper.GetChild(i1, 0);
然而, i1
是Grid
和i2
为Rectangle
。 这不是我所期望的。 我怎样才能到PlaylistControl.Header
?