2010-12-04 10 views
3

私は3つのボタンを持っています。 MouseOverとPressedの状態を表示するために3つのボタンのすべてに使用するスタイルを作成しました。どのボタンが選択/クリックされたかを示すロジックが必要です。ボタンがクリックされた場合、背景色は押された状態のままであるべきであり、他の2つのボタンは背景色にリセットされて通常状態であるべきである。以下は私のコードです。私は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

は、各ラジオボタンに同じGroupNameのを与え、あなたが必要な機能を得るでしょう。

+0

ありがとうございます。私はそれを試みます。 – vladc77

0

トグルボタン? 2つのボタンで構成されるトグルボタンユーザーコントロールを作成します。 1つは隠されて始まります。あなたが目に見えるものをクリックすると、それを隠して、隠れたものを表示します。次に、OnClickイベントの他のボタンを非表示にすることもできます。

+0

私の場合はトグルボタンを使用できません。 1つのボタンクリックで各ボタンの背景色の値を制御することは可能ですか? – vladc77

1

"IsPressed"の代わりに "IsFocused"プロパティを使用するように変更することができます

関連する問題