【问题标题】:XAML Checkbox Background and BorderBrush colors don't change on MouseOverXAML 复选框背景和 BorderBrush 颜色不会在 MouseOver 上更改
【发布时间】:2016-06-04 13:21:04
【问题描述】:

如何更改 BorderBrushBackground 上的复选框 MouseOver 的颜色?我试过这种方法,但它不起作用:

<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>

更改这些颜色适用于&lt;Trigger Property="IsChecked" Value="True"&gt; 条件。但它不适用于IsMouseOver

【问题讨论】:

    标签: wpf xaml checkbox mouseover


    【解决方案1】:

    您只是缺少 TargetName 来告诉它您将 setter 更改应用于哪个对象。因此,例如就拿你的;

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

    并成功;

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

    假设对象名可能是边框或其他东西,所以它会像

    <Border x:Name="ObjectNameToChangeStuff"/>
    

    希望这会有所帮助,干杯。

    PS - 因为我刚刚注意到。您将希望 ControlTemplate 中的这些触发器而不是您的样式,因此它将是 ControlTemplate.Triggers 而不是 Style.Triggers(因为您正在与 ControlTemplate 的元素交互)与 example 进行比较,以便您可以发现您的差异更轻松。 :)

    添加:

    因此,如果您的模板设置是这样的(伪);

    <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>
    

    ...那么您应该能够在依赖属性中传递一个值,以通过样式触发器与控件模板内的对象对话。

    【讨论】:

    • 我想知道IsChecked 条件如何使用简单的样式触发器更改控件颜色,但IsMouseOver 不能。写一个完整的控件模板是改变背景的唯一方法吗?
    • IsChecked 是控件的依赖属性,IsMouseOver 是控件上的事件,setter 必须点击控件模板内的对象。是的,我想是的。除非您将模板绑定背景属性到控件模板内的对象。那你就可以确定了。
    • 如果你愿意,我可以告诉你这样做,但不是通过我的手机 :)
    【解决方案2】:

    如果有人仍在尝试解决这个问题,您可以像这样设置和覆盖控件模板:

    <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 上的边框 = LightBlue
    • PressedMouseOver 的背景 = 黄色

    我已经稍微修改了这个example template 以考虑将鼠标悬停在边框颜色上。

    PS:这是一种非常幼稚的方法,肯定有办法将其浓缩,我也是 wpf 初学者 :P

    【讨论】:

      【解决方案3】:

      我试图覆盖 OnMouseOver 事件但没有成功。我同意 Yoghurt 的观点,即制作模板是最好的解决方案。但是,我建议使用 Unicode 字符,而不是使用 Path 绘制复选标记。请参阅维基百科了解不同的 Unicode 字符。 WPF 可能不支持某些较新的 unicode。

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

      【讨论】:

        猜你喜欢
        • 2016-04-25
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        • 2012-10-05
        • 1970-01-01
        • 2020-05-19
        • 2018-06-30
        相关资源
        最近更新 更多