1

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

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

我看到了几种方法可以实现它,但我真的不喜欢它们。

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

我可能会错过另一种方法吗?

4

1 回答 1

0

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

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

于 2012-05-16T17:24:53.683 回答