wpf - 自定义绘制复选框时,如何将椭圆形的填充颜色绑定到复选框的前景?
问题描述
我正在尝试制作一个看起来像一个小 LED 灯的 CheckBox:选中时颜色鲜艳,未选中时为灰色。在我的应用程序中,我需要具有不同 LED 颜色的状态灯。有人告诉我“WPF 方式”是找到几乎与您想要的完全一样的控件,然后对其进行自定义,这就是我正在尝试的。
我找到了 ColorToSolidColorBrushValueConverter 的代码,它允许我将 CheckBox 的前景色转换为在填充 LED 时使用的画笔。
老实说,我现在唯一需要做的就是完成 Ellipse 的 Fill 属性上的绑定路径,但我无法确定正确的绑定字符串。我尝试过诸如 Path=Foreground、Path=Target.Foreground、Path=TargetSource.Foreground、Path=Source.Foreground、Path=Parent.Foreground 之类的东西——但没有任何东西会导致 Ellipse 显示填充颜色。
在我的代码中,我注释掉了一个触发器,该触发器设置为在未选中该框时更改颜色,因此我可以确定触发器没有出现意外行为并阻止我看到颜色。为了简洁起见,我删除了这篇文章中注释掉的代码,这就是为什么所示的这种样式目前只能显示“已检查”状态的原因。
<converters:ColorToSolidColorBrushValueConverter x:Key="ColorToBrush" />
<Style x:Key="StyleDotCheckBox" TargetType="{x:Type CheckBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<StackPanel Orientation="Horizontal">
<Ellipse x:Name="ColorDot"
HorizontalAlignment="Center"
Stretch="UniformToFill"
StrokeLineJoin="Round"
Stroke="Black"
Fill="{Binding Path=Foreground, Converter={StaticResource ColorToBrush}}"
Margin="1"
/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我希望名为 ColorDot 的 Ellipse 会在某个时候用某种颜色填充,但它始终是空白的,我想是因为我还没有确定填充绑定的正确路径。
解决方案
Fill="{TemplateBinding Background}"
是您所需要的,因为您是从ControlTemplate
.
不需要转换器,因为Background
它已经是一个Brush
对象。
推荐阅读
- swift - CollectionView Reloaddata - 致命错误:索引超出范围
- ibm-mq - RFH2 - 标头的格式是什么?
- java - 如何为基于图像的 gitlab-ci 文件编写 dockerfile,以便我可以在构建阶段构建的质量检查阶段重用?
- c# - 如何使用公钥 RSA_OAEP_256 加密 JWT 有效负载
- azure-ad-b2c - 通过自定义策略 AAD 写入创建 Azure B2C 帐户将用户创建为已锁定
- stripe-payments - 自定义字体未加载到条带元素中
- ggplot2 - 制作箱线图时如何解决美学长度错误
- c++ - 非原子<> 指针上的原子操作能否比原子<> 更安全且更快?
- autodesk-forge - 纹理分辨率转换为 SVF
- ruby-on-rails - 删除多个图像活动存储阵列中的单个图像