2014-07-09 133 views
2

我試圖改變按鈕的外觀,當鼠標指針在它上面使用VisualStateManager。但它不起作用。請幫助!Windows Phone 8中的VisualStateManager

XAML

<Button x:Name="button" Background="AntiqueWhite" Grid.Row="2" Grid.Column="0" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave"> 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="ColorState"> 
      <VisualState x:Name="MouseEnter"> 
       <Storyboard Storyboard.TargetProperty="Background"> 
        <ColorAnimation To="Aquamarine" Duration="0:1:30"/> 
       </Storyboard> 
      </VisualState> 
      <VisualState x:Name="MouseLeave"/> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 
</Button> 

С#

private void button_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    bool bol = VisualStateManager.GoToState(this, MouseEnter.Name, true); 
} 

private void button_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    bool bol = VisualStateManager.GoToState(this, MouseLeave.Name, true); 
} 
+1

瓦你的代碼是?你有什麼嘗試?你有什麼問題?它不工作意味着什麼?你有錯誤嗎? ... –

+0

對不起,代碼不會立即添加。不,不會出現錯誤。但VisualStateManager.GoToState返回false。 –

+0

手機上沒有「鼠標指針」 - 使用輕敲或觸摸事件? –

回答

0

你VistualStateManager是不正確的。據我所知沒有「MouseEnter」,但有一個MouseOver

這是一個<Button>完整的ControlTemplate。我將MouseOver狀態的默認行爲註釋掉,並在Color Change Animation中進行編碼。你所要做的就是將任何Button的Style設置爲這個「Chubs_ButtonStyle」,並且它會突出顯示爲Aquamarine。希望這有助於。


<phone:PhoneApplicationPage.Resources> 
    <Style x:Key="Chubs_ButtonStyle" TargetType="Button"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/> 
     <Setter Property="Padding" Value="10,5,10,6"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Grid Background="Transparent"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"> 
            <Storyboard> 
             <!-- restore it back to original color --> 
             <ColorAnimation To="AntiqueWhite" Storyboard.TargetProperty="(ContentContainer.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" Duration="0"/> 
            </Storyboard> 
           </VisualState> 

           <VisualState x:Name="MouseOver"> 
            <Storyboard> 
             <!-- 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             --> 
             <ColorAnimation To="Aquamarine" Storyboard.TargetProperty="(ContentContainer.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" Duration="0"/> 
            </Storyboard> 
           </VisualState> 

           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}"> 
          <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</phone:PhoneApplicationPage.Resources> 

應用樣式的按鈕,像這樣

<Button x:Name="button" Background="AntiqueWhite" Grid.Column="0" Width="100" Height="100" Style="{StaticResource Chubs_ButtonStyle}"/> 

截圖在行動

enter image description here

+0

非常感謝! –