UWP Visual State Manager不会看到DataTemplate的内容

时间:2016-05-31 14:42:50

标签: xaml uwp visualstatemanager uwp-xaml adaptive-design

我的页面结构如下所示。

<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>

当我将 dateTB 文本块的setter添加到 VisualState.Setters 中以将其移动到Relative的左侧时小组,我收到一个错误说:

  

动画正在尝试修改名为“dateTB”的对象,但在页面中找不到此类对象。

添加setter的代码是:

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

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

1 个答案:

答案 0 :(得分:4)

这是名称范围的问题,对所有XAML UI框架都是通用的。您的VSM位于UserControlPage的名称范围内,TextBlock位于DataTemplate

Romasz解决您的问题,将VSM置于DataTemplate内,将您需要的所有内容放在一个名称范围内,是解决此问题的最佳方法。