UWP从模板化TabViewItem让孩子们(UWP Get Children from Templ

2019-10-30 01:41发布

我有使用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);

然而, i1Gridi2Rectangle 。 这不是我所期望的。 我怎样才能到PlaylistControl.Header

Answer 1:

我已经找到了解决办法。

由于ItemTemplate只会加载一次,我想这只是刷新绑定控件的数据剩下的时间,我可以注册的Loaded ,我需要的控件的事件。

而在这Loaded情况下,我能得到我从参数要控制sender ,并将其保存为一个类变量。 然后,我可以做我想做的事情。



文章来源: UWP Get Children from Templated TabViewItem