C# 能否使用VisualStateManager将悬停行为应用于XAML网格?

C# 能否使用VisualStateManager将悬停行为应用于XAML网格?,c#,windows-store-apps,winrt-xaml,uwp-xaml,visualstatemanager,C#,Windows Store Apps,Winrt Xaml,Uwp Xaml,Visualstatemanager,我有一个定义GridView的UWP XAML页面。每个GridView项都是一个网格。大概是这样的: <GridView Name="TheGridView" ItemsSource="{x:Bind stuff}"> <GridView.ItemTemplate> <DataTemplate x:DataType="more stuff"> <Grid Backgrou

我有一个定义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>
当鼠标悬停在某个项目上时,我想将其网格的背景颜色从黑色更改为其他颜色。我知道我可以将PointerEntered和PointerExit事件放在网格上,然后在我的代码中设置background属性,但这似乎是VisualStateManager的用途


但是,我不太明白如何让VisualStateManager为此工作。如果我在XAML中定义可视状态,那么我假设我仍然会连接到网格上的PointerEntered和PointerExit事件,但在我的代码中,我会调用GoToState来切换状态。但是,我不知道如何告诉GoToState XAML树中的哪个项需要更改其视觉状态。我认为我应该将悬停的网格项传递给GoToState的第一个参数,它在我的指针引用事件中作为“发送者”提供给我——但我不能,因为GoToState的第一个参数是控件,网格不是从控件派生的。

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

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