c# - 依赖属性 - 从另一个属性继承?
问题描述
我的 WPF 中有一个自定义依赖属性,UserControl
名为CustomForeground
.
如果UserControl.ForeGround
没有在CustomForeground
.
我正在使用以下代码,它可以工作,但肯定感觉有点hacky。
谁能确认确认是否有实现此依赖属性的“正确”方式?
public SolidColorBrush CustomForeground
{
get { return (SolidColorBrush)(GetValue(CustomForegroundProperty) ?? GetValue(ForegroundProperty)); }
set { SetValue(CustomForegroundProperty, value); }
}
注意 - 我省略了声明,DependencyProperty
因为它只是样板文件。
解决方案
您可以使用为 CustomForeground 属性设置一个绑定到其 Foreground 属性的 Setter 将 Style 添加到您的 UserControl。
除非 CustomForeground 属性值被另一个 Binding 或本地值或动画等替换,否则将使用 Binding。
<UserControl ...>
<UserControl.Style>
<Style>
<Setter
Property="local:MyUserControl.CustomForeground"
Value="{Binding Foreground, RelativeSource={RelativeSource Self}}"/>
</Style>
</UserControl.Style>
...
</UserControl>
推荐阅读
- firebase - 为什么我在 Android Studio 的 Flutter 项目中的 build.gradle 中出现此错误?
- python - Python 正则表达式不适用于给定的模式
- r - 在 R 中寻找合适的包进行聚类分析
- google-apps-script - 根据单元格值命名新工作表
- google-chrome-extension - Chrome 扩展程序:OAuth 验证请求问题
- mysql - 在mysql表中选择总和前10名
- python - 对于大型数组,是否有比 np.isin 更快的方法?
- reactjs - React 项目中自建模块的正确位置
- c - CS50 RECOVER 寻找迭代条件语句
- c# - 面临 NuGet 问题“'5.0.0+42a8779499c1d1ed2488c2e6b9e2ee6ff6107766' 不是有效的版本字符串。” 在安装软件包时