UWP 从模板化 TabViewItem 获取 Children
UWP Get Children from Templated TabViewItem
我使用以下方法获得了 TabViewItem:
var tab = PlaylistTabView.ContainerFromIndex(PlaylistTabView.SelectedIndex);
但是,我似乎没有访问正确的 children。
这是 TabView.ItemTemplate
的 xaml,其中 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
的 children:
var i1 = VisualTreeHelper.GetChild(tab, 0);
var i2 = VisualTreeHelper.GetChild(i1, 0);
然而,i1
是 Grid
而 i2
是 Rectangle
。那不是我所期望的。我怎样才能到达 PlaylistControl.Header
?
我找到了解决方案。
由于ItemTemplate
只会加载一次,其余时间我猜它只是刷新控件绑定的数据,我可以注册控件的Loaded
事件我需要
并且在那个Loaded
事件中,我可以从参数sender
中获取我想要的控件,并将其保存为class变量。那我就可以为所欲为了。
我使用以下方法获得了 TabViewItem:
var tab = PlaylistTabView.ContainerFromIndex(PlaylistTabView.SelectedIndex);
但是,我似乎没有访问正确的 children。
这是 TabView.ItemTemplate
的 xaml,其中 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
的 children:
var i1 = VisualTreeHelper.GetChild(tab, 0);
var i2 = VisualTreeHelper.GetChild(i1, 0);
然而,i1
是 Grid
而 i2
是 Rectangle
。那不是我所期望的。我怎样才能到达 PlaylistControl.Header
?
我找到了解决方案。
由于ItemTemplate
只会加载一次,其余时间我猜它只是刷新控件绑定的数据,我可以注册控件的Loaded
事件我需要
并且在那个Loaded
事件中,我可以从参数sender
中获取我想要的控件,并将其保存为class变量。那我就可以为所欲为了。