自定义控件边框画笔属性不一致?
我在 WPF 中创建了自己的控件。最初,我将其创建为用户控件,但发现执行此操作的首选方法是创建一个继承自控件的类,然后将我各自的 xaml 放入 Generic.xaml
内的控件模板中。
当它在我的 exe 中时效果很好,但是当我将它移动到 dll 时,边框从我的控件中消失了。我的控件基于文本框,几乎是文本框控件模板的复制和粘贴,并添加了用户可以单击的按钮。
我已经确定了控件模板中不起作用的相关部分,即下面的 BorderBrush="{TemplateBinding Border.BorderBrush}"
位以及下一行。
<Style TargetType="{x:Type local:ButtonBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ButtonBox}">
<mwt:ListBoxChrome
Background="{TemplateBinding Panel.Background}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
BorderThickness="{TemplateBinding Border.BorderThickness}"
RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}"
RenderFocused="{TemplateBinding UIElement.IsKeyboardFocusWithin}"
Name="Bd"
SnapsToDevicePixels="True">
我了解模板绑定,但不明白为什么我们要绑定到 Border.BorderBrush
。我们绑定的border
在哪里?可视化树没有显示属于我的控件一部分的边框。如果我用硬编码值替换这两行,那么我会得到一个边框。我怀疑 exe 的 dll 中可能缺少某些内容,例如样式或适用于边框的内容?
预先感谢您的回复以及任何花时间阅读的人。 干杯, 迈克尔
I have created my own control in WPF. Initially I created it as a usercontrol but found the preferred way to do this is to create a class which inherits from control and then place my respective xaml in a control template inside Generic.xaml
.
This worked fine when it was in my exe but when I moved it to a dll the border disappeared from my control. My control is based off a textbox and is a pretty much a copy and paste of the textbox's control template with the addition of a button the user can click.
I've identified the relevant part of the control template that is not working which is the BorderBrush="{TemplateBinding Border.BorderBrush}"
bit below as well as the next line.
<Style TargetType="{x:Type local:ButtonBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ButtonBox}">
<mwt:ListBoxChrome
Background="{TemplateBinding Panel.Background}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
BorderThickness="{TemplateBinding Border.BorderThickness}"
RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}"
RenderFocused="{TemplateBinding UIElement.IsKeyboardFocusWithin}"
Name="Bd"
SnapsToDevicePixels="True">
I understand template binding but I don't understand why we are binding to Border.BorderBrush
. Where is the border
that we are binding to? The visual tree shows no border that is part of my control. If I replace these 2 lines with hard coded values then I get a border. I suspect there might be something missing from the dll that the exe has such as a style or something that applies to Border??
Thanks in advance for any replies and anyone who took the time to read.
Cheers,
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于解决了这个问题。回答我的第一个问题“当控件模板中没有定义边框时,为什么我们在这段代码中使用 Border.BorderBrush”:
Border.BorderBrush 中的边框之所以存在,是因为依赖属性是在 Border 类上定义的。尽管 Control 具有 BorderBrush 的依赖属性,但该属性实际上是在 Border 中定义的。即,Border 中是这样定义的
,Control 中是这样使用的。
这里的关键点是 Control 没有定义自己的 BorderBrush 依赖属性,而是使用 AddOwner 将自己与现有的依赖属性关联起来。这就是为什么它在控件模板中被定义为 Border.BorderBrush,即使文本框在其控件模板中没有边框。
我的第二个问题“这个值在哪里设置”的答案是它是在文本框的默认样式中设置的。我可以通过执行以下操作来查看文本框的默认样式:
运行此代码后,我们可以在样式中看到两个属性都是硬编码的。这对我来说似乎很奇怪,但这显然就是这样做的方式。
在我将其复制粘贴到我的控件的样式中后,一切都按预期工作。
简单嘿? :-)))
I finally got this sorted out. To answer my first question "Why are we using Border.BorderBrush in this code when there is no border defined in the control template":
The border in Border.BorderBrush is there because the dependency property is defined on the Border class. Although Control has a dependency property of BorderBrush the property is actually defined in Border. ie, this is how is it defined in Border
and this is how it is used in Control
The key point here is that Control does not define its own dependency property of BorderBrush, instead it uses AddOwner to associate itself with the existing dependency property. This is why it is defined as Border.BorderBrush in the control template even though the textbox does not have a border in its control template.
The answer to my second question "where is this value set", is that it is set in the textbox's default style. I can get a look at the default style for a textbox by doing this:
Once we run this code we can see in the style were both properties are hard coded. This seems odd to me but this is apparently the way it has been done.
After I copy pasted that into the style for my control everything worked as expected.
Simple hey? :-)))