【问题标题】:In WPF, can I somehow use TemplateBinding to set the CornerRadius of a templated Button?在 WPF 中,我可以以某种方式使用 TemplateBinding 来设置模板化按钮的 CornerRadius 吗?
【发布时间】:2012-05-16 17:18:52
【问题描述】:

我有一个想要重用的 Button ControlTemplateemplate,除了它的 Border 的 CornerRadius,我想从 TemplatedParent 获取它,以便我可以在 Button 声明中指定它。

很遗憾,CornerRadius 不是 Button 的属性,所以编译器不喜欢它。

我看到了几种可以成功的方法,但我不太喜欢它们。

  1. 滥用我不使用的字段(例如,MinWidth?)来传达这个新的半径。不确定这是否可行,因为 int 不完全是 CornerRadius。
  2. 创建 Button 的子类以添加 CornerRadius 并使用它。

我可能会遗漏另一种方法吗?

【问题讨论】:

标签: c# wpf button controltemplate cornerradius


【解决方案1】:

我能想到的唯一另一件事是为 CornerRadius 创建一个 AttachedProperty 并将其附加到按钮上,然后绑定到该按钮上。我不确定绑定到 AttachedProperty 是否可以通过 TemplatedParent 工作。

此外,与创建 AttachedProperty 相比,创建派生 Button 类的代码可能更少。所以可能不值得。

【讨论】:

  • 您的答案似乎与@eren-ersonmez 指出的欺骗线程上的答案相匹配,所以我想这是我能得到的最好的答案。
猜你喜欢
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多