问题描述
我使用了 TabViewItem:
I have got the TabViewItem using:
var tab = PlaylistTabView.ContainerFromIndex(PlaylistTabView.SelectedIndex);
但是,我似乎没有访问正确的孩子.
However, it doesn't seem that I am accessing the correct children.
这是TabView.ItemTemplate
的xaml,其中PlaylistControl
是我的自定义控件,基本上只是一个ListView
:
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>
我正在尝试使用以下方法访问 tab
的子项:
I am trying to access the children of tab
using:
var i1 = VisualTreeHelper.GetChild(tab, 0);
var i2 = VisualTreeHelper.GetChild(i1, 0);
然而,i1
是 Grid
而 i2
是 Rectangle
.这不是我所期望的.如何访问 PlaylistControl.Header
?
However, i1
is Grid
and i2
is Rectangle
. That's not what I expect. How can I get to the PlaylistControl.Header
?
推荐答案
我找到了解决方案.
由于ItemTemplate
只会被加载一次,剩下的时间我猜只是刷新绑定到控件的数据,我可以注册Loaded
事件我需要的控件.
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.
并且在那个 Loaded
事件中,我可以从参数 sender
中获取我想要的控件,并将其保存为类变量.然后我可以为所欲为.
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.
这篇关于UWP 从模板化 TabViewItem 获取子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!