XAML Checkbox背景和BorderBrush颜色不会在MouseOver上更改

时间:2016-02-22 22:21:52

标签: wpf xaml checkbox mouseover

如何更改BorderBrush上的CheckBox BackgroundMouseOver颜色?我试过这种方式,但它不起作用:

<CheckBox Style="{DynamicResource checkBox}">CheckBoxText</CheckBox>

这是我的风格:

<Style TargetType="{x:Type CheckBox}" x:Key="checkBox">

    <!-- This part changes the colors -->
    <Setter Property="BorderBrush" Value="LightGray" />
    <Setter Property="BorderThickness" Value="3" />
    <Setter Property="Background" Value="LightGray" />

    <Style.Triggers>

        <Trigger Property="IsMouseOver" Value="True">

            <!-- This part is not changing the colors -->
            <Setter Property="BorderThickness" Value="3" />
            <Setter Property="Background" Value="Gray" />
            <Setter Property="BorderBrush" Value="Gray" />

        </Trigger>

    </Style.Triggers>

</Style>

更改这些颜色适用于<Trigger Property="IsChecked" Value="True">条件。但它不适用于IsMouseOver

3 个答案:

答案 0 :(得分:0)

您只是错过了TargetName来告诉它您应用setter更改的对象。所以例如,只需要你的;

<Setter Property="Background" Value="Gray" />

并制作它;

<Setter TargetName="ObjectNameToChangeStuff" Property="Background" Value="Gray" />

假设objectname可能是边框或其他东西,那么它就像

<Border x:Name="ObjectNameToChangeStuff"/>

希望这会有所帮助,欢呼。

PS - 因为我刚才注意到了。您希望ControlTemplate中的这些触发器不是您的样式,因此ControlTemplate.Triggers而不是Style.Triggers(因为您与ControlTemplate的元素交互)与{{3}比较,这样你就可以更容易地发现你的差异。 :)

增加:

因此,如果您的模板设置类似于此(伪);

<Style x:Key="{x:Type CheckBox}" TargetType="CheckBox">
  <!-- Blah blah setter stuff for defaults -->
  <Setter Property="Background" Value="DefaultColorForThisThing"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="CheckBox">

       <BlahBlah Stuff/>...

      <!-- Whatever CLR object that supports it, for example -->
      <Border Background="{TemplateBinding Background}"/>

      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

...然后你应该能够在依赖属性中传递一个值,通过样式触发器与控件模板内的对象进行通信。

答案 1 :(得分:0)

如果有人仍在尝试找出答案,则可以像这样设置样式并覆盖控件模板:

<Style x:Key="checkboxTemplate" TargetType="{x:Type CheckBox}">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="FocusVisualStyle" Value="{DynamicResource CheckBoxFocusVisual}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <BulletDecorator Background="Transparent">
                    <BulletDecorator.Bullet>
                        <Border x:Name="Border" Width="13" Height="13" CornerRadius="0" BorderThickness="1">
                            <Border.BorderBrush>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStopCollection>
                                            <GradientStop Color="Blue" Offset="0.0" />
                                            <GradientStop Color="Blue" Offset="1.0" />
                                        </GradientStopCollection>
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Border.BorderBrush>
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStopCollection>
                                            <GradientStop Color="Orange" />
                                            <GradientStop Color="Orange" Offset="1.0" />
                                        </GradientStopCollection>
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Border.Background>
                            <Grid>
                                <Path Visibility="Collapsed" Width="7" Height="7" x:Name="CheckMark" SnapsToDevicePixels="False" StrokeThickness="2" Data="M 0 0 L 7 7 M 0 7 L 7 0">
                                    <Path.Stroke>
                                        <SolidColorBrush Color="Black" />
                                    </Path.Stroke>
                                </Path>
                                <Path Visibility="Collapsed" Width="7" Height="7" x:Name="InderminateMark" SnapsToDevicePixels="False" StrokeThickness="2" Data="M 0 7 L 7 0">
                                    <Path.Stroke>
                                        <SolidColorBrush Color="Black" />
                                    </Path.Stroke>
                                </Path>
                            </Grid>
                        </Border>
                    </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).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="Yellow" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="Yellow" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="LightBlue" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="LightBlue" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="Yellow" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="Yellow" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="LightBlue" />
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="LightBlue" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled" />
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="CheckMark">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unchecked" />
                            <VisualState x:Name="Indeterminate">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="InderminateMark">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter Margin="4,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" RecognizesAccessKey="True" />
                </BulletDecorator>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

位置:

  • 边界=蓝色
  • 背景=橙色
  • 刻度/交叉=黑色
  • PressedMouseOver上的边界=浅蓝
  • PressedMouseOver上的背景=黄色

我对此example template进行了略微修改,以解决悬停在边框颜色上的问题。

PS:这是一种非常幼稚的方法,肯定有一种方法可以将其压缩下来,我也是wpf初学者:P

答案 2 :(得分:0)

我尝试覆盖OnMouseOver事件,但没有成功。我同意Yoghurt的观点,即制作模板是最佳解决方案。但是,我建议您使用Unicode字符,而不是使用路径绘制对勾标记。有关不同的unicode字符,请参见Wikipedia。 WPF可能不支持某些较新的unicode。

https://en.wikipedia.org/wiki/Check_mark