UWP VisualStateManager PointerOver不起作用

时间:2019-08-13 13:38:29

标签: xaml uwp visualstatemanager visualstates visualstategroup

我正在模仿Groove Music的行为,当鼠标悬停在专辑封面上时,它会显示阴影效果(在我的代码中,这是整个DataTemplate)。但是我的VisualStateManager似乎不起作用。有任何想法吗?我已经以编程方式实现了该功能,但我想使用xaml进行练习。

---更新---

将Stackpanel更改为Grid仍无法使事情正常工作。

    <GridView
        Grid.Row="1"
        Margin="10"
        IsItemClickEnabled="True"
        ItemsSource="{x:Bind Albums}">
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:GridAlbumView">
                <Grid
                    Width="180"
                    Height="240"
                    Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <controls:DropShadowPanel
                        x:Name="AlbumShadowPanel"
                        VerticalContentAlignment="Top"
                        BlurRadius="15"
                        OffsetX="4"
                        OffsetY="4"
                        ShadowOpacity="0"
                        Color="Black">
                        <Image Source="{x:Bind Cover}" />
                    </controls:DropShadowPanel>
                    <TextBlock
                        Margin="0,5,0,0"
                        Grid.Row="1"
                        FontWeight="SemiBold"
                        MaxLines="2"
                        Text="{x:Bind Name}"
                        TextAlignment="Left"
                        TextWrapping="Wrap"
                        Visibility="{x:Bind Name, Converter={StaticResource AlbumNameVisibilityConverter}}" />
                    <TextBlock
                        Grid.Row="2"
                        FontSize="12"
                        Foreground="Gray"
                        Text="{x:Bind Artist}"
                        TextAlignment="Left" />
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="AlbumShadowPanel" Storyboard.TargetProperty="ShadowOpacity">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="1" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

1 个答案:

答案 0 :(得分:0)

那是因为StackPanel不是控件。仅控件支持VisualStates。您可以只创建一个透明控件,将其置于所有内容之上(假设您不需要与控件内部的内容进行交互)。抱歉,我知道这可能会造成混淆。

public class MyOverlayControl : Control
{
    public MyOverlayControl() : base()
    {
        Background = new SolidColorBrush(Colors.Transparent);
        HorizontalAlignment = HorizontalAlignment.Stretch;
        VerticalAlignment = VerticalAlignment.Stretch;
        PointerEntered += (s, e) => VisualStateManager.GoToState(this, nameof(PointerEntered), true);
        PointerExited += (s, e) => VisualStateManager.GoToState(this, nameof(PointerExited), true);
    }
}