WPF:ContentPresenter 上的 TextTrimming
有没有一种简单的方法可以让 TextTrimming 与 ContentPresenter 一起使用?
我有 TextBlock 和 AccessText 的隐式样式,它们的 TextTrimming 设置为 CharacterEllipsis,但 ContentPresenter 没有拾取它。我可以将 ContentPresenter 更改为 AccessText 或 TextBlock 并将其设置在那里,但模板仅处理文本内容。
有什么建议吗?
谢谢!
Is there a simple way to just get TextTrimming to work with a ContentPresenter?
I have implict styles for TextBlock and AccessText that have TextTrimming set to CharacterEllipsis, but it's not picked up by the ContentPresenter. I can change the ContentPresenter to an AccessText or TextBlock and set it there, but then the template only handles text content.
Any suggestions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果元素是在控件的模板中定义的,则不会应用从 UIElement(而非 Control)派生的元素的隐式样式除非在应用程序资源中定义隐式样式。对于 ContentPresenter 使用的 TextBlock 也是如此。
例如,在下面的 XAML 中,最终用于呈现按钮内容的 TextBlock 将不会获取隐式 Style:
如果您采用完全相同的 Style 并将其移动到应用程序的资源中,那么两者都会红色:
因此您可以将隐式样式移动到应用程序资源,这通常不是一个好主意。或者您可以根据您的具体情况自定义显示。这可以包括添加隐式 DataTemplate,或自定义控件的模板。
如果您可以提供更多信息,那么会更容易知道哪种方法是最好的方法。
Implicit Styles for elements that derive from UIElement, but not Control, are not applied if the element is defined in a control's Template unless the implict Style is defined in the application Resources. The same holds true for TextBlocks used by ContentPresenter.
For example, in the following XAML the TextBlock that is ultimately used to present the button's content will not get the implicit Style:
If you take that exact same Style and move it to the application's Resources, then both will be red:
So you can either move your implicit Style to application resources, which is generally not a good idea. Or you can customize the display for the specific scenario you have. This can include adding an implicit DataTemplate, or customizing a control's Template.
If you can provide more information, then it would be easier to know which is the best approach.
感谢 James Nugent 的这个要点:“WPF 样式将字符省略号放在按钮内容上,而不用替换ContentPresenter 带有 TextBlock,从而失去支持访问键的能力。”
这对我有用:
Thanks to this Gist by James Nugent: "WPF style which puts character ellipsis on button contents without replacing the ContentPresenter with a TextBlock and thus losing the ability to support access keys."
This worked for me: