Silverlight 按钮 Mouse Over 类似悬停效果

Silverlight button Mouse Over like hover effect

我创建了带样式的按钮,但在创建按钮样式后,它失去了按钮的默认效果,当我直接将属性放入按钮时,我将获得这些默认效果,例如当我单击时我可以看到蓝色 background.I也尝试放置 Visual Manager,但它不起作用。请有人可以帮助我知道我做错了什么

我的按钮样式:

<Style TargetType="Button" x:Key="MenuButtonStyle">
  <Setter Property="HorizontalAlignment" Value="Stretch"/>
  <Setter Property="Foreground" Value="Black"/>
  <Setter Property="FontFamily" Value="Sitka Heading"/>
  <Setter Property="FontSize" Value="20"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Normal"/>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <ColorAnimation Duration="0" Storyboard.TargetName="ButtonTextElement"
                                  Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                  To="Blue"/>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" 
                                                 Storyboard.TargetName="normalImage">
                    <DiscreteObjectKeyFrame KeyTime="0">
                      <DiscreteObjectKeyFrame.Value>
                        <Visibility>Collapsed</Visibility>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" 
                                                 Storyboard.TargetName="mouseOverImage">
                    <DiscreteObjectKeyFrame KeyTime="0">
                      <DiscreteObjectKeyFrame.Value>
                        <Visibility>Visible</Visibility>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="MouseOver">
                <Storyboard>
                  <ColorAnimation Duration="0" Storyboard.TargetName="ButtonTextElement"
                                  Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                  To="Blue"/>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" 
                                                 Storyboard.TargetName="normalImage">
                    <DiscreteObjectKeyFrame KeyTime="0">
                      <DiscreteObjectKeyFrame.Value>
                        <Visibility>Collapsed</Visibility>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" 
                                                 Storyboard.TargetName="mouseOverImage">
                    <DiscreteObjectKeyFrame KeyTime="0">
                      <DiscreteObjectKeyFrame.Value>
                        <Visibility>Visible</Visibility>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border BorderBrush="Black" BorderThickness="0,0,0,0.5" Margin="30,0,0,0"
                  Grid.ColumnSpan="2"/>
          <TextBlock x:Name="ButtonTextElement" 
                     Text="{TemplateBinding Content}" Margin="30,0"
                     Foreground="{TemplateBinding Foreground}" Grid.Column="0" 
                     VerticalAlignment="{TemplateBinding VerticalAlignment}" />
          <Image x:Name="normalImage" Source="/Assets/menu-arrow-left.png" 
                 Grid.Column="1" Stretch="None" HorizontalAlignment="Right"
                 Margin="0,0,30,0" />
          <Image x:Name="mouseOverImage" Source="/Assets/menu-arrow-left-hover.png"
                 Grid.Column="1" Stretch="None" HorizontalAlignment="Right" 
                 Visibility="Collapsed" Margin="0,0,30,0" />
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

VisualStateManager我也是这样改的

<ColorAnimation Storyboard.TargetName="MouseOverVisualElement" 
                            Storyboard.TargetProperty="TextBlock.Foreground" To="Red" />

My Button Tag

<Button Style="{StaticResource MenuButtonStyle}" Content="Home"/>

请查看此页面:

https://msdn.microsoft.com/en-us/library/cc278069(VS.95).aspx

并查看此页面: http://samples.msdn.microsoft.com/Silverlight/SampleBrowser/index.htm#/?sref=CustomTemplateSamples&id=4

有模板鼠标悬停示例

确保 VisualStateManager.VisualStateGroups 是您的 ControlTemplate 根元素的直接子元素。在您的示例中,您的根元素是 Border,因此将 VisualStateManager 内容直接放在 Border 标签下。

您的模板有几个问题。首先:您必须确保由 Storyboard.TargetName 标识的元素及其由 Storyboard.TargetProperty 标识的 属性(您想要更改)确实有意义。你可以改变一个SolidColorBrush的颜色,你可以用一个SolidColorBrush换一个Textblock.Foreground属性,但是你不能直接设置前景色属性的颜色,因为前景实际上是 Brush(不是 Color)。第二:如果您覆盖控件的模板,则必须提供原始模板中的所有 VisualStates,这意味着您还必须定义 FocusStates。 这是一个模板,可以执行您想要执行的操作:

<ControlTemplate TargetType="Button">
        <Grid>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal"/>
                    <VisualState x:Name="MouseOver">
                        <Storyboard>

                            <ColorAnimation
                                Duration="0"
                                Storyboard.TargetName="ButtonTextElement"
                                Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                To="Red"/>

                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Pressed">
                        <Storyboard>
                            <ColorAnimation
                                Duration="0"
                                Storyboard.TargetName="Background"
                                Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                To="#FF6DBDD1"/>
                            <ColorAnimation
                                Duration="0"
                                Storyboard.TargetName="ButtonTextElement"
                                Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                To="Red"/>


                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Disabled">
                        <Storyboard>
                            <DoubleAnimation
                                Duration="0"
                                Storyboard.TargetName="DisabledVisualElement"
                                Storyboard.TargetProperty="Opacity"
                                To=".55"/>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
                <VisualStateGroup x:Name="FocusStates">
                    <VisualState x:Name="Focused">
                        <Storyboard>
                            <DoubleAnimation
                                Duration="0"
                                Storyboard.TargetName="FocusVisualElement"
                                Storyboard.TargetProperty="Opacity"
                                To="1"/>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Unfocused"/>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Border
                x:Name="Background"
                Background="White"/>
            <TextBlock
                x:Name="ButtonTextElement"
                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                Text="{TemplateBinding Content}"
                FontSize="20" FontFamily="Sitka Heading" 
                Foreground="Black"/>

            <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" Opacity="0" IsHitTestVisible="false" />
            <Rectangle x:Name="FocusVisualElement" Margin="1" Stroke="#FF6DBDD1" StrokeThickness="1" Opacity="0" IsHitTestVisible="false" />
        </Grid>
    </ControlTemplate>

default button style can be found on msdn.