如何在嵌套元素上设置视觉状态

本文关键字:设置 视觉 状态 元素 嵌套 | 更新日期: 2023-09-27 18:31:49

我正在尝试使用VisualStateManager为属性设置值。问题是我需要在嵌套元素(称为变量SizedWrapGrid)上设置此值,但它不响应相应的状态。这个想法是当用户更改平板电脑的方向(横向到纵向)时,应该更改此元素的方向。

<GroupStyle.Panel>
    <ItemsPanelTemplate>
        <VariableSizedWrapGrid x:Name="variableSizedWrapGrid" Orientation="Vertical" Background="Blue" Width="660" ItemHeight="120" ItemWidth="220" Margin="0,0,80,0">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup>
                    <VisualState x:Name="FullScreenPortrait">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Orientation)">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="Horizontal" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </VariableSizedWrapGrid>
    </ItemsPanelTemplate>
</GroupStyle.Panel>

我正在为 Windows 8 开发一个 Windows Metro 应用程序。

如何在嵌套元素上设置视觉状态

我相信

这就是LayoutAwarePage的用途。如果您将 LayoutAwarePage 作为基类,并且希望控件在平板电脑旋转时切换 VisualStates,则将控件的 Loaded 事件挂接到 LayoutAwarePage 上的 StartLayoutUpdates 方法。

如您所见,每当状态更改时,它都会运行"InvalidateVisualState",它将遍历接收 LayoutUpdate 的所有控件,并手动将其 VisualStateManager 设置为适当的状态。

因此,在你拥有的代码中,你需要为景观设置一个 VisualState——我相信它被称为 FullScreenLandscape,但我不记得了——然后将 StartLayoutUpdates 添加到 VariableSizedWrapGrid 控件的 Loaded 事件中。这是假设您的页面是LayoutAwarePage。老实说,如果你想这样做,你应该这样做。出于这个原因,他们包含了所有这些样板代码。

希望有帮助。