在Windows 8中将可视状态添加到数据模板 [英] Adding Visual States to a Data Template in Windows 8

查看:92
本文介绍了在Windows 8中将可视状态添加到数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将鼠标悬停效果添加到Windows 8应用程序中。具体来说,我正在尝试将其添加到绑定到GridView的DataTemplates中。但是,目前没有任何反应,我尝试遵循Microsoft教程,但是其中大多数教程已经过时或针对不同版本的XAML。

I am trying to add a Mouse Over effect to my Windows 8 application. Specifically I'm trying to add it to DataTemplates bound to a GridView. However, Currently, nothing is happening, I've tried to follow the Microsoft tutorials but most of those are either out of date or for different versions of XAML.

我的代码看起来像这样:

My code looks like this:

<DataTemplate x:Key="GameTileTemplate">
    <Grid x:Name="grid" Width="173" Height="173" RenderTransformOrigin="0.5,0.5" >
        <Grid.Clip>
            <RectangleGeometry Rect="0,0,173,173"/>
        </Grid.Clip>
        <Image Grid.RowSpan="3" Stretch="UniformToFill"/>
        <Grid x:Name="DataPanel" Margin="-173,0,0,0" Grid.RowSpan="3" RenderTransformOrigin="0.5,0.5" Width="346" HorizontalAlignment="Left" VerticalAlignment="Top" Height="173">
            <!--There is more here-->
        </Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStateGroup">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="PointerEntered">
                    <Storyboard>
                        <DoubleAnimation From="1" To="0" Duration="00:00:02" 
                             Storyboard.TargetName="DataPanel" 
                             Storyboard.TargetProperty="Opacity">
                        </DoubleAnimation>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</DataTemplate>

我的DataPanel的不透明度不变。我是否需要其他代码? Microsoft教程用于ControlTemplate,因为我的模板是DataTemplate,这是否会导致错误?

The Opacity of my DataPanel does not change. Do I need some other code somewhere? The Microsoft Tutorial was for a ControlTemplate, is this causing the error since my Template is a DataTemplate?

推荐答案

您在您的XAML中提供的Xml问题不会自己解决。仅定义视觉状态是不够的。您还需要某种代码来调用 VisualStateManager.GoToState

Xaml you provided in your question is not going to work on its own. Simply defining visual states is not enough. You also need some kind of code to call VisualStateManager.GoToState.

在您的特殊情况下,解决方案是不向 DataTemplate 添加视觉状态,但要为 GridViewItem创建自定义模板。通常, GridViewItem 负责用通用的指针,选择,拖放状态装饰GridView中的元素。

In your particular case solution is not to add visual states to DataTemplate, but to create custom template for GridViewItem. In general GridViewItem is responsible for decorating elements inside the GridView with common pointer, selection, drag and drop states.

这篇关于在Windows 8中将可视状态添加到数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆