wpf - 绑定到 xaml 中另一个控件样式定义的默认属性值
问题描述
我有一个CustomControl
并且想要拥有TextBox
默认样式定义的默认值。我不知道这是否可能。
这是我迄今为止尝试过的,但没有成功。但在这里你可以看到我的想法。
我的默认实现来更改默认样式TextBox
(例如在 app.xaml 中将其应用于全局)
<Style TargetType="TextBox">
<Setter Property="BorderThickness" Value="2"/>
</Style>
在这里我想得到'2'的值。
<Style TargetType="controls:CustomControl">
<Setter Property="BorderThickness" Value="{Binding Source={x:Static TextBox.BorderThicknessProperty}}"/>
</Style>
解决方案
BorderThickness
a 的属性的默认值在运行时最终应用于 a 的实例的TextBox
默认样式中定义。TextBox
TextBox
所以你不能做这样的事情:
<Style TargetType="controls:CustomControl">
<Setter Property="BorderThickness" Value="{Binding Source={x:Static TextBox.BorderThicknessProperty}}"/>
</Style>
TextBox
...除非您绑定到使用默认样式的实际实例。
您可以查看默认模板TextBox
并简单地复制其默认BorderThickness
属性值1
:
<Style TargetType="controls:CustomControl">
<Setter Property="BorderThickness" Value="1"/>
</Style>
显然,您还可以从多个 绑定到一个类的属性Style
,例如:
<Style TargetType="controls:CustomControl">
<Setter Property="BorderThickness" Value="{Binding Thickness, Source={StaticResource settings}}"/>
</Style>
推荐阅读
- cypress - 赛普拉斯 - 生成令牌
- python - 如何在 python 中使用 np.unique 从列中选择唯一值
- bash - BATS assert_failure 测试无法识别出口 1?
- r - 我可以使用 R 中的 DBI 库将一个 SQLite 数据库附加到另一个数据库吗?
- javascript - 为什么我的可重用函数不起作用,我在没有变量的情况下执行逻辑并且函数正确运行
- security - Webhook 安全性 - HMAC 与回调 URL 中的令牌
- python - 关于使用 setitem 的地图功能的建议
- amazon-web-services - 将基础设施指标从 AWS Batch 获取到 DataDog
- java - 在 Java 中将 Avro 字节转换为字符串
- ruby - 无法将我的哈希作为参数传递给类构造函数