选择单选按钮时如何不允许onMouseOver / Out状态?

时间:2010-12-07 19:43:54

标签: wpf wpf-controls

我想知道在选中/选择单选按钮时是否可以暂停onMouserOver状态。我有一个原型,我通过点击重新设置的单选按钮来切换内容。选中单选按钮时,我需要保持选定状态不变。现在,当鼠标翻转/翻转发生时,它会改变背景颜色,因为在未选择按钮时应该更改。下面是代码。提前谢谢。

<StackPanel>
    <RadioButton x:Name="one"  Content="one" IsChecked="True" GroupName="intro" Style="{DynamicResource RadioButtons}" Style="{DynamicResource RadioButtons}" />
    <RadioButton x:Name="two"  Content="two" IsChecked="True" GroupName="intro" Style="{DynamicResource RadioButtons}" Style="{DynamicResource RadioButtons}" />
    <RadioButton x:Name="three"  Content="three" IsChecked="True" GroupName="intro" Style="{DynamicResource RadioButtons}" Style="{DynamicResource RadioButtons}" />
</StackPanel>

风格:

<Style x:Key="IntroRadioButtons" TargetType="{x:Type RadioButton}">
  <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  <Setter Property="Background" Value="#F4F4F4"/>
  <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="{x:Type RadioButton}">

      <Border HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
        Margin="{TemplateBinding Padding}" 
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
        x:Name="borderRadioButon" BorderBrush="{x:Null}" BorderThickness="0" 
        Background="#FFFFB343" Width="90" Height="90" >

      <VisualStateManager.VisualStateGroups>
       <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal"/>
        <VisualState x:Name="MouseOver">
         <Storyboard>
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="borderRadioButon">
           <EasingColorKeyFrame KeyTime="0" Value="#FFFD7713"/>
          </ColorAnimationUsingKeyFrames>
         </Storyboard>
        </VisualState>
        <VisualState x:Name="Pressed">
         <Storyboard>
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="borderRadioButon">
           <EasingColorKeyFrame KeyTime="0" Value="#FFFD7713"/>
          </ColorAnimationUsingKeyFrames>
         </Storyboard>
        </VisualState>
        <VisualState x:Name="Disabled"/>
       </VisualStateGroup>
       <VisualStateGroup x:Name="CheckStates">
        <VisualState x:Name="Checked">
         <Storyboard>
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="borderRadioButon">
           <EasingColorKeyFrame KeyTime="0" Value="#FFFD7713"/>
          </ColorAnimationUsingKeyFrames>
         </Storyboard>
        </VisualState>
        <VisualState x:Name="Unchecked"/>
        <VisualState x:Name="Indeterminate"/>
       </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>

      <ContentPresenter RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
       Margin="0,0,0,4" HorizontalAlignment="Center" VerticalAlignment="Bottom" 
       TextBlock.FontSize="{DynamicResource PrimaryFontSize}" 
       TextBlock.FontFamily="{DynamicResource PrimaryFontFamily}"    
         />

     </Border>

     <ControlTemplate.Triggers>
      <Trigger Property="HasContent" Value="true">
       <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
       <Setter Property="Padding" Value="4,0,0,0"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
       <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
      </Trigger>
     </ControlTemplate.Triggers>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>

1 个答案:

答案 0 :(得分:1)

类似问题here。当它们针对同一控件的同一属性时,似乎是两个VisualStateGroup的冲突。使用与链接相同的方法,它似乎正在工作:添加了两个边框,每个组一个。如果有更好的方法来解决这个问题,那么我对那个问题也感兴趣:)

<Style x:Key="IntroRadioButtons" TargetType="{x:Type RadioButton}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="#F4F4F4"/>
    <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RadioButton}">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="borderRadioButon">
                                        <EasingColorKeyFrame KeyTime="0" Value="#FFFD7713"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="borderRadioButon">
                                        <EasingColorKeyFrame KeyTime="0" Value="#FFFD7713"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="borderRadioButon2">
                                        <EasingColorKeyFrame KeyTime="0" Value="#FFFD7713"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unchecked"/>
                            <VisualState x:Name="Indeterminate"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  
                            Margin="{TemplateBinding Padding}"  
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                            x:Name="borderRadioButon" BorderBrush="{x:Null}" BorderThickness="0"  
                            Background="#FFFFB343" Width="90" Height="90">
                        <Border HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  
                                Margin="0"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                x:Name="borderRadioButon2" BorderBrush="{x:Null}" BorderThickness="0"  
                                Background="Transparent" Width="90" Height="90">
                            <ContentPresenter RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  
                                            Margin="0,0,0,4" HorizontalAlignment="Center" VerticalAlignment="Bottom"  
                                            TextBlock.FontSize="{DynamicResource PrimaryFontSize}"  
                                            TextBlock.FontFamily="{DynamicResource PrimaryFontFamily}"/>
                        </Border>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasContent" Value="true">
                        <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
                        <Setter Property="Padding" Value="4,0,0,0"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>