【问题标题】:Can you use VisualStateManager to apply hover behaviors to XAML grid?您可以使用 VisualStateManager 将悬停行为应用于 XAML 网格吗?
【发布时间】:2018-10-22 09:30:09
【问题描述】:

我有一个定义 GridView 的 UWP XAML 页面。各个 GridView 项目都是一个网格。像这样的:

    <GridView Name="TheGridView" ItemsSource="{x:Bind stuff}">

        <GridView.ItemTemplate>
            <DataTemplate x:DataType="more stuff">
                <Grid Background="{StaticResource TheBlackColor}">

                    ...stuff here...                    

                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>

   </GridView>

当鼠标悬停在某个项目上时,我想更改网格的背景颜色(从 TheBlackColor 到其他颜色)。我知道我可以将 PointerEntered 和 PointerExited 事件放在 Grid 上,然后在我后面的代码中设置背景属性,但这似乎是 VisualStateManager 的用途。

但是,我不知道如何让 VisualStateManager 工作。如果我在 XAML 中定义视觉状态,那么我假设我仍然会连接到 Grid 上的 PointerEntered 和 PointerExited 事件,但在我后面的代码中,我会调用 GoToState 来切换状态。但是,我不知道如何告诉 GoToState XAML 树中的哪个项目需要更改其视觉状态。我认为我只是将悬停的网格项传递给 GoToState 的第一个参数(它在我的 PointerEntered 事件中作为“发送者”给我)——但我不能因为 GoToState 的第一个参数是一个 Control 和Grid 不是从 Control 派生的。

【问题讨论】:

    标签: c# windows-store-apps winrt-xaml uwp-xaml visualstatemanager


    【解决方案1】:

    根据您的要求,您可以使用XAML Behaviors 来实现此功能。请参考以下代码。

    <Page
        x:Class="VisualStateTest.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:VisualStateTest"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
        xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
        xmlns:Media="using:Microsoft.Xaml.Interactions.Media"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid>
            <Interactivity:Interaction.Behaviors>
                <Core:EventTriggerBehavior EventName="PointerEntered">
                    <Core:ChangePropertyAction PropertyName="Background">
                        <Core:ChangePropertyAction.Value>
                            <SolidColorBrush Color="Red"/>
                        </Core:ChangePropertyAction.Value>
                    </Core:ChangePropertyAction>
                </Core:EventTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </Grid>
    </Page>
    

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 2011-06-01
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多