【问题标题】:UWP Visual State Manager doesn't see content of DataTemplateUWP 视觉状态管理器看不到 DataTemplate 的内容
【发布时间】:2016-09-29 15:34:12
【问题描述】:

我的页面结构如下图。

<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 文本块的设置器添加到 VisualState.Setters 以将其移动到相对的左侧面板,我收到一条错误消息:

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

添加setter的代码是:

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

有没有办法通过这个页面结构的可视状态管理器来控制这个文本块?

【问题讨论】:

  • 您的 TextBlockDataTemplate 内,这就是您得到异常的原因。
  • @Romasz 是的,就是这样。但是,当我删除 DataTemplate 时,我收到错误消息说 ItemTemplate 不支持类型 Grid。你知道如何制作一个可以从 VisualStateManager 看到的 ItemTemplate 吗?
  • 您是否尝试过将您的 visualstatemanager 放在数据模板中的网格下?
  • @Romasz 我成功了。感谢您的评论,这是因为 DataTemplate,我发现我必须将 DataTemplate 的内容放在 UserControl 中,然后将 VisualStateManager 放在 DataTemplate 的网格中。感谢您的支持。

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


【解决方案1】:

这是所有 XAML UI 框架共有的名称范围问题。您的 VSM 位于 UserControlPage 的名称范围内,而 TextBlock 位于 DataTemplate 中。

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

【讨论】:

  • 如果我有一个DataTemplate,它的Content 是一个自定义UserControl(基本上是一个ListView)。而ListView 有一个Header,由一个ScrollHeader 组成。而ScrollHeader 是控件的Grid。我想更改Grid 中控件的视觉状态。我应该把 VSM 放在哪里?我把它放在ScrollHeader 中,但它不起作用。
  • 您应该问自己的问题并提供更多详细信息,但同样适用 - 如果您需要视觉状态 - 您可能应该创建一个单独的控件并将 VSM 放在根面板上 (Grid)
猜你喜欢
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多