2017-08-08 52 views
1

現在我只想在鼠標懸停時將按鈕的背景更改爲#f5f5f5。
WPF具有觸發功能,可以輕鬆完成。
雖然我聽說UWP沒有觸發任何更多,但與其他函數來代替,像這樣的問題:
Trigger element (XAML) is not supported in a UWP project如何在鼠標懸停在UWP時更改按鈕的背景?

我DataTriggerBehavior做了它作爲教程:

<ControlTemplate TargetType="Button" x:Key="ButtonControlTemplate"> 
      <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Name="ButtonBorder" Background="{TemplateBinding Background}">     
       <ContentPresenter FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Foreground="{TemplateBinding Foreground}"></ContentPresenter> 
      </Border> 
      <Interactivity:Interaction.Behaviors> 
       <Core:DataTriggerBehavior Binding="{Binding PointerMovedEvent, ElementName=ButtonBorder}" Value="true"> 
        <Core:ChangePropertyAction TargetObject="{Binding ElementName=ButtonBorder}" PropertyName="Background" Value="#f5f5f5" /> 
       </Core:DataTriggerBehavior> 
      </Interactivity:Interaction.Behaviors> 
     </ControlTemplate> 


程序可以成功工作,但不能更改背景。
哦,我的上帝。
我也嘗試過VisualState,但是我找不到教程,所以我不知道如何使用它。
更重要的是,我幾乎不知道爲什麼微軟不再使用觸發器。
你能幫我解決我的問題嗎?
非常感謝!

回答

2

您可以使用觸發器或XAML Behavior SDK中的行爲,但我會用Button的自定義樣式代替。您只需在default style內的PointerOver狀態中更改其顏色。

<VisualState x:Name="PointerOver"> 
    <Storyboard> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="#f5f5f5"/> 
     </ObjectAnimationUsingKeyFrames> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPointerOver}"/> 
     </ObjectAnimationUsingKeyFrames> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}"/> 
     </ObjectAnimationUsingKeyFrames> 
     <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/> 
    </Storyboard> 
</VisualState> 

或者,您也可以覆蓋ButtonBackgroundPointerOverApp.xaml內。

​​
+1

感謝您再次幫助我。我想我必須馬上學習更多關於VisualState的知識。 –

相關問題