2010-12-04 128 views
3

我有3個按鈕。我創建了一個用於所有三個按鈕的樣式,以顯示MouseOver和Pressed狀態。我需要邏輯來指示哪個按鈕被選中/點擊。如果按鈕被點擊,那麼背景顏色應該保持爲按下狀態,而其他兩個按鈕應該被重置爲背景顏色以作爲正常狀態。以下是我的代碼。我想知道是否有可能在XAML中實現所有功能,或者如何在代碼中實現它?謝謝,我提前。如何改變點擊任何一個按鈕的背景顏色?

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="test3.MainWindow" 
x:Name="Window" 
Title="MainWindow" 
Width="640" Height="480"> 

<Window.Resources> 
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true" Background="#FFFFE640"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"> 
            <Storyboard> 
             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground"> 
              <EasingColorKeyFrame KeyTime="0" Value="#FFC8B432"/> 
             </ColorAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground"> 
              <EasingColorKeyFrame KeyTime="0" Value="#FFBCAA31"/> 
             </ColorAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <ContentPresenter RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,0,0,4" HorizontalAlignment="Center" VerticalAlignment="Bottom"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsKeyboardFocused" Value="true"> 
         </Trigger> 
         <Trigger Property="ToggleButton.IsChecked" Value="true"> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="#ADADAD"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <Storyboard x:Key="OnMouseOneEnter"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button_One"> 
      <EasingColorKeyFrame KeyTime="0" Value="#FFDAC326"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 
    <Storyboard x:Key="OnMouseOneLeave"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button_One"> 
      <EasingColorKeyFrame KeyTime="0" Value="#FFFFE640"/> 
     </ColorAnimationUsingKeyFrames> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button_Two"> 
      <EasingColorKeyFrame KeyTime="0" Value="#FF85781C"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 
    <Storyboard x:Key="OnMoseOneClick"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button_One"> 
      <EasingColorKeyFrame KeyTime="0:0:0.2" Value="#FFDAC326"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 
</Window.Resources> 

<Grid> 
    <StackPanel x:Name="LayoutRoot"> 
     <Button x:Name="button_One" HorizontalAlignment="Left" Width="90" Height="90" Content="One" Style="{DynamicResource ButtonStyle1}" Cursor="Hand" Margin="0,0,0,4" /> 
     <Button x:Name="button_Two" HorizontalAlignment="Left" Width="90" Height="90" Content="Two" Style="{DynamicResource ButtonStyle1}" Cursor="Hand" Margin="0,0,0,4" /> 
     <Button x:Name="button_Three" HorizontalAlignment="Left" Width="90" Height="90" Content="Two" Style="{DynamicResource ButtonStyle1}" Cursor="Hand" Margin="0,0,0,4" /> 
    </StackPanel> 
</Grid> 

回答

0

聽起來像你我真的喜歡你後單選按鈕的功能。

http://www.wpftutorial.net/RadioButton.html

給每個單選按鈕相同的組名,你會得到你所需要的功能。而不是

+0

謝謝。我會嘗試。 – vladc77 2010-12-05 05:40:28

0

切換按鈕?創建一個由兩個按鈕組成的切換按鈕用戶控件。一開始隱藏。當您單擊可見的人物時,隱藏它並顯示隱藏的人物。然後,您還可以在OnClick事件中隱藏/顯示其他按鈕。

+0

在我的情況下,我不能使用切換按鈕。是否有可能通過單擊一個按鈕來控制每個按鈕的背景顏色值? – vladc77 2010-12-04 19:45:20

1

你可以改變它使用「IsFocused」 PROPERT,「IsPressed」

相關問題