UWP Visual State Manager 看不到 DataTemplate 的内容

UWP Visual State Manager doesn't see content of DataTemplate

我的页面结构如下图

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualState x:Name="VisualStateNarrow">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="1"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>

                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="VisualStateWide">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="800"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>


                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>


    <Grid  Background="White">

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Pivot x:Name="PivotPlatform" Margin="0" ItemsSource="{Binding PivotItems}" Grid.Row="2">


        <Pivot.HeaderTemplate>
            <DataTemplate>
                <StackPanel Height="0" Width="0">
                    <TextBlock Text="{Binding}" />
                </StackPanel>
            </DataTemplate>
        </Pivot.HeaderTemplate>

        <Pivot.ItemTemplate>
            <DataTemplate>
                <Grid xmlns:uwp="using:AmazingPullToRefresh.Controls">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <uwp:PullToRefreshAdorner.Extender>
                        <uwp:PullToRefreshExtender RefreshRequested="PullToRefreshExtender_RefreshRequested" />
                    </uwp:PullToRefreshAdorner.Extender>

                    <RelativePanel x:Name="contentPanel"  Grid.Row="0" Margin="10 -30 10 10">
                        <TextBlock Name="titleTB" Text="{Binding Title}" FontSize="12" 
                                   RelativePanel.AlignLeftWithPanel="True"
                                   RelativePanel.AlignTopWithPanel="True"/>
                        <TextBlock Name="totalTB" Text="{Binding Total}" FontSize="18"
                               RelativePanel.AlignLeftWithPanel="True"
                               RelativePanel.Below="titleTB" />
                        <ProgressBar Name="progressBar" Value="{Binding ProgressValue}" Width="100" Foreground="{StaticResource currentThemeColor}"
                                     RelativePanel.AlignLeftWithPanel="True" RelativePanel.Below="totalTB" 
                                     Margin="0 5 0 0"/>
                        <TextBlock Name="dateTB" Text="{Binding Date}" FontSize="16" 
                               RelativePanel.AlignRightWithPanel="True"
                               RelativePanel.AlignTopWithPanel="True" />
                    </RelativePanel>

                        <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
                            <Charting:Chart Grid.Row="1" x:Name="LineChart"
                                    Margin="10" >
                                <Charting:LineSeries Title="" IndependentValuePath="Name"  DependentValuePath="Amount" 
                                             IsSelectionEnabled="False" ItemsSource="{Binding Result}" />

                            </Charting:Chart>
                        </ScrollViewer>

                    </Grid>
            </DataTemplate>
        </Pivot.ItemTemplate>

    </Pivot>

当我将 setter for dateTB 文本块添加到 [=31 时=] 将其移动到相关面板的左侧,我收到一条错误消息:

An animation is trying to modify an object named 'dateTB', but no such object can be found in the Page.

添加 setter 的代码是:

<Setter Target="dateTB.(RelativePanel.AlignLeftWithPanel)" Value="True"/>

有没有办法通过具有此页面结构的 Visual State Manager 控制此文本块?

这是所有 XAML UI 框架共有的名称范围问题。您的 VSM 在 UserControlPage 的名称范围内,而 TextBlockDataTemplate.

Romasz 将 VSM 放入 DataTemplate 中的问题解决方案将您需要的一切都放在一个名称范围内,是解决此问题的最佳方案。