【问题标题】:Windows Store Apps: change the background of GridViewItem on PointerOverWindows 应用商店应用程序:在 PointerOver 上更改 GridViewItem 的背景
【发布时间】:2013-07-24 17:18:44
【问题描述】:

我有一个绑定到以下模型的 gridview

    class Item
    {
    string Title;
    string ImagePath
    string ImagePathPressed;
    }

其中 ImagePath 和 ImagePathPressed 是应用内图像的路径。

现在我希望我的网格视图项在鼠标悬停时将其背景从 ImagePath 中的值更改为 ImagePathPressed

中的值

如何做到这一点?

【问题讨论】:

    标签: windows-8 windows-runtime windows-store-apps winrt-xaml


    【解决方案1】:

    如果您将这些变量作为属性并在您的类上实现 INotifyPropertyChanged 会更好。在gridView 的mouseOver 事件中,将ImagePath 更改为ImagePathPressed,这将反映ImagePath 的变化。我认为在您的鼠标悬停事件中,您可以获得鼠标指针所在的项目。

    【讨论】:

      【解决方案2】:

      按照此链接获取实现 GridViewItem 样式的指南

      http://msdn.microsoft.com/en-us/library/windows/apps/jj709915.aspx

      您应该将您的类成员实现为可绑定属性,然后将 PointerOver 状态实现为上述链接中的指南。 我建议您应该创建两张图像(一张用于正常状态,另一张用于悬停状态) 例如:

      <Setter Property="Template">
          <Setter.Value>
              <ControlTemplate TargetType="GridViewItem">
                  <Border x:Name="OuterContainer">
                      <Grid>
                          <Image x:Name="NormalImage" Source="{Binding ImagePath}"/>
                          <Image x:Name="PressImage" Source="{Binding ImagePathPressed}" Opacity="0"/>
                      <VisualStateManager.VisualStateGroups>
                          <VisualStateGroup x:Name="CommonStates">
                              <VisualState x:Name="Normal"/>
                              <VisualState x:Name="PointerOver">
                                  <Storyboard>
                                      <DoubleAnimation Storyboard.TargetName="PressImage"
                                                       Storyboard.TargetProperty="Opacity"
                                                       Duration="0"
                                                       To="1" />
                                  </Storyboard>
                              </VisualState>
                              <VisualState x:Name="Pressed">...
      

      【讨论】:

      • 谢谢,我用了类似的方法,请查看我上面的答案
      【解决方案3】:

      好的,我知道了

      我实现了一个这样的控制模板:

      <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="GridViewItem">
                      <Border x:Name="OuterContainer" Tag={Binding}>
                              <Border.Resources>
                              <!-- Define brush resources for both states-->
                                  <ImageBrush x:Key="MouseOverBrush"  ImageSource="{Binding Tag.ImagePathPressed, ElementName=OuterContainer}" Stretch="None" />
                                  <ImageBrush x:Key="DefaultBrush"  ImageSource="{Binding Tag.ImagePath, ElementName=OuterContainer}" Stretch="None" />
                              </Border.Resources>
                          <Grid>
                          <VisualStateManager.VisualStateGroups>
                              <VisualStateGroup x:Name="CommonStates">
                                  <VisualState x:Name="Normal"/>
                                  <VisualState x:Name="PointerOver">
                                      <Storyboard>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ReorderHintContent" >
                                                  <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MouseOverBrush}" />
                                              </ObjectAnimationUsingKeyFrames>
                                      </Storyboard>
                                  </VisualState>
                                  <VisualState x:Name="Pressed">
                                  .
                                  .
                                  .
                                  <Grid x:Name="ReorderHintContent" Tag="{Binding}" DataContext="{Binding}" >
                                  <Grid.Background>
                                  <!-- Default background-->
                                      <ImageBrush x:Name="BGBrush" ImageSource="{Binding Tag.ImagePath, ElementName=ReorderHintContent}" Stretch="None" Opacity="0" />
                                  </Grid.Background>
      

      我必须为边框和网格设置标签才能访问模型的属性

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多