Silverlight按钮Mouse Over就像悬停效果一样

Mil*_*ind 5 c# silverlight xaml windows-8.1 windows-phone-8.1

我创建了带有样式的按钮但是在创建按钮样式后它失去了按钮的默认效果,当我直接将属性放在按钮中时,我会得到那些默认效果,就像我点击时我可以看到蓝色背景.我也尝试放置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>
Run Code Online (Sandbox Code Playgroud)

我也像这样改变VisualStateManager

<ColorAnimation Storyboard.TargetName="MouseOverVisualElement" 
                            Storyboard.TargetProperty="TextBlock.Foreground" To="Red" />
Run Code Online (Sandbox Code Playgroud)

My Button Tag

<Button Style="{StaticResource MenuButtonStyle}" Content="Home"/>
Run Code Online (Sandbox Code Playgroud)

Mar*_*tin 1

您的模板存在几个问题。首先:您必须确保由 标识的元素Storyboard.TargetName及其由 标识的属性(您想要更改)Storyboard.TargetProperty确实有意义。您可以更改 a 的颜色SolidColorBrush,并且可以将 a 用于SolidColorBrush属性Textblock.Foreground,但不能直接设置 foreground 属性颜色,因为 foreground 实际上是 a Brush(而不是 a 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>
Run Code Online (Sandbox Code Playgroud)

默认按钮样式可以在 msdn 上找到