2016-11-09 85 views
1

我正在为单选按钮创建样式,并在单击时更改单选按钮的颜色。但是,我想使它成为这样的样子,当你单选按钮时,它也会改变颜色。我已经实现了基本功能,但是当单选按钮已经被选中(并且因此具有不同的颜色)时,如果它获得mouseover,则颜色变为该新颜色,并且当mouseover完成(鼠标离开它)时,颜色可以追溯到原始的未选定颜色。WPF Mouseover重置控制颜色

有没有一种方法让它知道什么时候点击了正确的颜色? (该的TargetName Border仅仅是一个边界类)

<Style x:Key="MyRadioButton" TargetType="{x:Type RadioButton}"> 
    <Setter Property="SnapsToDevicePixels" Value="true" /> 
    <Setter Property="FocusVisualStyle" Value="{DynamicResource RadioButtonFocusVisual}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type RadioButton}"> 
       <BulletDecorator Background="Transparent"> 

        <BulletDecorator.Bullet> 
         <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> 
          <Border x:Name="Border" Margin="1" CornerRadius="5" HorizontalAlignment="Center" 
            Width="50" Background="White" Padding="2" SnapsToDevicePixels="true"> 
           <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> 
          </Border> 
         </Grid> 
        </BulletDecorator.Bullet> 

        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal" /> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" 
             Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> 
             <EasingColorKeyFrame KeyTime="0" Value="{StaticResource SelectedUnfocusedColor}" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" 
              Storyboard.TargetProperty="(Panel.Background).(GradientStop.Color)"> 
             <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlLightColor}" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="CheckStates"> 
          <VisualState x:Name="Checked"> 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" 
             Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> 
             <EasingColorKeyFrame KeyTime="0" Value="{StaticResource SelectedBackgroundColor}" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Unchecked" /> 
          <VisualState x:Name="Indeterminate" /> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
       </BulletDecorator> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

回答

1

使用此Style,并放置两个差异RadioButton s到测试Unchecked状态:

<Style TargetType="{x:Type RadioButton}"> 
      <Setter Property="SnapsToDevicePixels" Value="true" /> 
      <Setter Property="FocusVisualStyle" Value="{DynamicResource RadioButtonFocusVisual}" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type RadioButton}"> 
         <BulletDecorator Background="Transparent"> 

          <BulletDecorator.Bullet> 
           <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> 

            <Border x:Name="Border" Margin="1" CornerRadius="5" HorizontalAlignment="Center" 
             Width="50" Padding="2" SnapsToDevicePixels="true"> 
             <Border.Background> 
              <VisualBrush> 
               <VisualBrush.Visual> 
                <Grid> 
                 <Border x:Name="InnerBorder" Margin="1" CornerRadius="5" HorizontalAlignment="Center" 
            Width="50" Background="Transparent" Padding="2" SnapsToDevicePixels="true"/> 
                 <Border x:Name="OuterBorder" Margin="1" CornerRadius="5" HorizontalAlignment="Center" 
            Width="50" Background="Transparent" Padding="2" SnapsToDevicePixels="true"/> 

                </Grid> 
               </VisualBrush.Visual> 
              </VisualBrush> 
             </Border.Background> 
              <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> 
             </Border> 

           </Grid> 
          </BulletDecorator.Bullet> 

          <VisualStateManager.VisualStateGroups> 

           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal" /> 
            <VisualState x:Name="MouseOver"> 
             <Storyboard> 
              <ColorAnimationUsingKeyFrames Storyboard.TargetName="OuterBorder" 
                Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> 
               <EasingColorKeyFrame KeyTime="0" Value="Red" /> 
              </ColorAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Pressed"> 
            </VisualState> 
            <VisualState x:Name="Disabled"> 
             <Storyboard> 
              <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" 
              Storyboard.TargetProperty="(Panel.Background).(GradientStop.Color)"> 
               <EasingColorKeyFrame KeyTime="0" Value="Pink" /> 
              </ColorAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
           <VisualStateGroup x:Name="CheckStates"> 
            <VisualState x:Name="Checked"> 
             <Storyboard> 
              <ColorAnimationUsingKeyFrames Storyboard.TargetName="InnerBorder" 
             Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> 
               <EasingColorKeyFrame KeyTime="0" Value="Yellow" /> 
              </ColorAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Unchecked"> 
             <Storyboard> 
              <ColorAnimationUsingKeyFrames Storyboard.TargetName="InnerBorder" 
             Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> 
               <EasingColorKeyFrame KeyTime="0" Value="Transparent" /> 
              </ColorAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 

            <VisualState x:Name="Indeterminate" /> 
           </VisualStateGroup> 

          </VisualStateManager.VisualStateGroups> 
         </BulletDecorator> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style>