如何使所有文本大写/大写?
我希望 TextBlock
、Label
、MenuItem.Header
中的所有文本都以大写形式显示。 这些字符串取自 ResourceDictionary
例如:
<TextBlock Text="{StaticResource String1}"/>
<MenuItem Header="{StaticResource MenuItemDoThisAndThat}"/>
等(也适用于 Label
和其他控件)
我无法使用值转换器,因为没有绑定。我不想在字典本身中将字符串设置为大写。
I want all texts in TextBlock
, Label
, MenuItem.Header
to be displayed in upper case.
The strings are taken from a ResourceDictionary
e.g.:
<TextBlock Text="{StaticResource String1}"/>
<MenuItem Header="{StaticResource MenuItemDoThisAndThat}"/>
etc. (also for Label
and other controls)
I cannot use a value converter because there is no binding. I don't want to make the strings upper case in the dictionary itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为这对您有用
对于字体大写枚举 https://msdn.microsoft.com/en-us/library/system.windows.fontcapitals(v=vs.110).aspx
I think this will work for you
For font capitals enumerations https://msdn.microsoft.com/en-us/library/system.windows.fontcapitals(v=vs.110).aspx
您仍然可以使用转换器,只需在绑定源中设置文本值:
You still can use a converter, just set the textvalue in the source of the binding :
您可以在 TextBox 中使用标签 CharacterCasing,而不是使用转换器,但在您的情况下,它不适用于 TextBlock。
Rather than using a converter, you can use the tag CharacterCasing in a TextBox but in your case, it doesn't work on a TextBlock.
要完成 Peter 的回答(我的编辑已被拒绝),您可以使用如下转换器:
C#:
XAML:
To complete Peter's answer (my edit has been rejected), you can use a converter like this:
C#:
XAML:
我为此创建了一个附加属性和转换器。您可能已经有了转换器,因此请将我对 CaseConverter 的引用替换为您拥有的任何实现。
附加属性只是一个布尔值,如果您希望它是大写的,您可以设置它(显然您可以将其扩展为选择样式的可枚举值)。当属性更改时,它会根据需要重新绑定 TextBlock 的 Text 属性,并添加转换器。
当属性已经绑定时,可能需要做更多的工作 - 我的解决方案假设它是一个简单的路径绑定。但它可能还需要复制源等。但是我觉得这个例子足以表达我的观点。
这是附加的属性:
I created an attached property and converter for this. You probably already have the converter, so replace my reference to CaseConverter to whatever implementation you have.
The attached property is just a boolean that you set if you want it to be uppercase (you could obviously extend this to instead be an enumerable for a selection of styles). When the property changes, it rebinds the TextBlock's Text property as needed, adding in the converter.
A little more work might need to be done when the property is already bound - my solution assumes it's a simple Path binding. But it may need to also duplicate the source, etc. However I felt this example is enough to get my point across.
Here's the attached property:
这并没有严格回答问题,但确实提供了产生相同效果的技巧。
我相信许多人在这里找到了自己的路,正在寻找如何以一种风格来做到这一点。 TextBlock 在这里有点棘手,因为它不是 Control,而是 FrameworkElement,因此您无法定义 Template 来完成此任务。
最有可能需要使用全部大写文本的标题或类似的内容,其中标签的使用是合理的。我的解决方案是:
请注意,这确实禁用了 Label 的一些默认行为,并且仅适用于文本,因此我不会将其定义为默认值(无论如何,没有人希望所有标签都大写)。当然,当您需要这种样式时,您必须使用 Label 而不是 TextBlock。另外,我不会在其他模板中使用它,而只是严格作为主题样式。
This does not strictly answer the question but does provide a trick to cause the same effect.
I believe many finding their way here are looking how to do this with a style. TextBlock is a bit tricky here because it is not a Control but a FrameworkElement and therefore you can not define a Template to do the trick.
The need to use all uppercase text is most likely for headings or something like that where use of Label is justified. My solution was:
Note that this does disable some of the default behavior of Label, and works only for text, so I would not define this as default (no one probably wants all labels uppercase anyway). And of course you must use Label instead of TextBlock when you need this style. Also I would not use this inside of other templates, but only strictly as a topic style.
您可以使用以下属性对 TextBox 控件中的所有输入进行大小写:
要应用于整个应用程序中的所有 TextBox 控件,请为所有 TextBox 控件创建一个样式:
You can case all input into TextBox controls with the following property:
To apply to all TextBox controls in the entire application create a style for all TextBox controls: