【WPF】HandyControl组件库设置ToggleButton滑动开关控件高亮时的颜色不生效

遇到的问题:

HandyControl的<ToggleButton Style="{StaticResource ToggleButtonSwitch}"/>中的固定样式如下图:
在这里插入图片描述
现在的需求是想要修改该控件在被选中时的颜色为绿色,可以使用hc:VisualElement.HighlightBrush,但这个属性却一直不生效,这让我非常疑惑,

<ToggleButton Height="24" Width="48"  IsChecked="{Binding Remark}" Style="{StaticResource ToggleButtonSwitch}" hc:VisualElement.HighlightBrush="{DynamicResource DarkSuccessBrush}" />

解决办法:

直到在网上查到一个博客说是.net的问题,.net4.5后的版本需要在App.xaml.cs中加上:

public App()
{
    FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = false;
}

加上后重新生成项目就可以了:
在这里插入图片描述

参考链接:
https://blog.csdn.net/u010975266/article/details/83148079?spm=1001.2014.3001.5502

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中,可以通过修改件的 Style 来自定义件的禁用样式。以下是一个简单的示例: ```xml <Window.Resources> <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" /> <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" /> <Style x:Key="CustomButtonStyle" TargetType="Button"> <Setter Property="Background" Value="Blue" /> <Setter Property="Foreground" Value="White" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}" /> <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Button Content="Click me" Style="{StaticResource CustomButtonStyle}" /> </Grid> ``` 在此示例中,我们创建了一个名为 `CustomButtonStyle` 的样式,并为其设置了默认的背景和前景颜色。然后,在件模板中,我们使用一个 Border 来包装按钮的内容,并设置了一些绑定属性。在触发器中,我们检查件的 `IsEnabled` 属性是否为 False,并使用自定义颜色设置背景和前景。注意,我们使用了一个静态资源来定义禁用颜色,这样我们就可以在应用程序中的任何地方重复使用这些颜色。 希望这个例子可以帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值