2017-04-25 191 views
2

我想在按下和釋放時更改按鈕的背景。我在C#通用Windows平臺編碼。我正在嘗試以下,但失敗。如何更改按鈕和背景之間的按鈕背景?

private void pay_LostFocus(object sender, RoutedEventArgs e) 
    { 
     pay.Background = new SolidColorBrush(Windows.UI.Colors.Pink); 
    } 

    private void one_FocusEngaged(Control sender, FocusEngagedEventArgs args) 
    { 
     one.Background = new SolidColorBrush(Windows.UI.Colors.White); 
    } 
+0

所以,你要當按下和粉紅色時沒有按下按鈕是白色的? – AVK

回答

0

您可以使用指針事件要做到這一點,但你必須通過AddHandler方法來手動添加處理程序,你就還需要能夠處理以前處理的指針事件,以確保UI住宿是一致的。

您可以使用此代碼:

MyButton.AddHandler(
    UIElement.PointerEnteredEvent, // The target event 
    new PointerEventHandler((s, e) => 
    { 
     // Handle your background here 
    }), 
    true); // Include previously handled events 

這些都是你需要使用的事件:UIElement.PointerEnteredEventUIElement.PointerExitedEventUIElement.PointerCaptureLostEventUIElement.PointerCanceledEventUIElement.PointerReleasedEvent

您需要根據源事件調整按鈕的背景,因爲其中一些方法在用戶開始按下按鈕時觸發,而其他方法在釋放按鈕時觸發。

編輯:這裏是添加處理一個例子,當按鈕被釋放:

foreach (RoutedEvent target in new[] { UIElement.PointerExitedEvent, UIElement.PointerCaptureLostEvent, 
             UIElement.PointerCanceledEvent, UIElement.PointerReleasedEvent }) 
{ 
    MyButton.AddHandler(target, new PointerRoutedEventArgs((s, e) => 
    { 
     MyButton.Background = new SolidColorBrush(Colors.Red); 
    }), true); 
} 

做的PointerPressedEvent與其他刷(當按鈕被按下)相同的,你應該是所有組。

1

只是修改您的按鈕的樣式,特別是壓制 VisualState。示例:

<Style x:Key="MyButtonStyle" TargetType="Button"> 
    <Setter Property="Background" Value="{ThemeResource ButtonBackground}"/> 
    <Setter Property="Foreground" Value="{ThemeResource ButtonForeground}"/> 
    <Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}"/> 
    <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/> 
    <Setter Property="Padding" Value="8,4,8,4"/> 
    <Setter Property="HorizontalAlignment" Value="Left"/> 
    <Setter Property="VerticalAlignment" Value="Center"/> 
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/> 
    <Setter Property="FontWeight" Value="Normal"/> 
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/> 
    <Setter Property="UseSystemFocusVisuals" Value="True"/> 
    <Setter Property="FocusVisualMargin" Value="-3"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Grid x:Name="RootGrid" Background="{TemplateBinding Background}"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"> 
           <Storyboard> 
            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="PointerOver"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPointerOver}"/> 
            </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> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="White"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPressed}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <PointerDownThemeAnimation Storyboard.TargetName="RootGrid"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundDisabled}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushDisabled}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundDisabled}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

然後使用它:

<Button Content="Something" Style="{StaticResource MyButtonStyle}" Padding="20" Background="Pink"/>