WPF 模板:AdornedElement 未显示!
我想通过使用带有额外元素的模板将一些元素添加到文本框,并将原始文本框插入到正确的位置。我正在尝试使用 AdornedElementPlaceholder 就像您在制作 Validation.ErrorTemplate 时所做的那样,但是 AdornedElement 没有显示。我尽可能地简化了示例:
<TextBox Text="Border me with my Template">
<TextBox.Template>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border BorderBrush="Green" BorderThickness="1">
<AdornedElementPlaceholder/>
</Border>
</ControlTemplate>
</TextBox.Template>
</TextBox>
结果只是在应该是我的文本框的空间周围有一个绿色框!
I want to add some elements to a TextBox by using a Template with the extra elements and the original TextBox inserted in the right spot. I'm trying to use the AdornedElementPlaceholder just like you would do when making a Validation.ErrorTemplate But the AdornedElement is not showing up. I have simplified the example as much as possible:
<TextBox Text="Border me with my Template">
<TextBox.Template>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border BorderBrush="Green" BorderThickness="1">
<AdornedElementPlaceholder/>
</Border>
</ControlTemplate>
</TextBox.Template>
</TextBox>
The result is just a green box around the space that should be my textbox!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,事实并非如此。然而,这并不比这困难多少。如果您想通过更改控件模板在文本框周围添加绿色边框,它看起来会更像这样:
其中的重要部分是名为 PART_ContentHost 的 ScrollViewer。当应用模板时,文本框会寻找那个人,并使用它来承载文本、插入符号等。我认为您缺少的是,当您覆盖元素的模板时,您将覆盖整个 控制模板。不幸的是,你不能只改变一点点,但这就是事情的真相。
当然,如果您想保持 TextBox 的原始外观和感觉,例如它仍然看起来像 Win7 TextBox,则需要在 ControlTemplate 中做更多的事情。
就其价值而言,如果您正在讨论使用Adorner,那么您尝试应用的模板似乎可以工作。它与 WPF 中验证模板的工作方式类似,但那完全是另一回事了。
哦,对于将边框更改为绿色的更简单的方法,您应该能够仅在 TextBox 本身上设置 BorderBrush 属性。当然,我真的不知道你到底想做什么。
--
华泰
尘土飞扬
Unfortunately, that's just not how it works. However, it's not much more difficult than that. If you want to add a green border around a text box by changing the control template, it's going to look a bit more like this:
The important part in there is the ScrollViewer named PART_ContentHost. The TextBox looks for that guy when the template is applied and uses it to host the text, caret, etc. What I think you're missing is that when you override the Template for an element, you're overriding the entire control template. It's unfortunate that you can't just change bits and pieces, but that's the truth of the matter.
Of course, if you want to maintain the original look and feel of the TextBox, such that it still looks like a Win7 TextBox for instance, you'd need to do a bit more in the ControlTemplate.
For what it's worth, it looks like the template you were trying to apply would work if you're talking about using an Adorner. It's similar to how the validation templates work in WPF, but that's a whole-nother story.
Oh, and for a much simpler way to change the border to green, you should be able to just set the BorderBrush property on the TextBox itself. Of course, I really don't know exactly what you're going for.
--
HTH
Dusty
我最终做了一个基于 HeaderedContent 控件的自定义控件。
它将允许用户单击或将鼠标悬停在某些内容上,然后显示包含某些其他或相同内容的气泡。
用法:
用户控件的代码:
I ended up doing a Custom Control based on a HeaderedContent Control.
It will allow the user to click or hoover over some content and then show a bubble containing some other or the same content.
Usage:
The code for the User control: