如何在 WPF 中的 TextBlock 上应用 CharacterCasing?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22142745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply CharacterCasing on a TextBlock in WPF?
提问by GibboK
I have a specific TextBlock in a WPF application. I need to make the text Uppercase for that specifc TextBlock.
我在 WPF 应用程序中有一个特定的 TextBlock。我需要为该特定 TextBlock 制作文本大写。
Trying with the following code I get this error:
尝试使用以下代码,我收到此错误:
{"'TextUpperCase' is not a valid value for property 'Style'."}
Any idea how to solve it?
知道如何解决吗?
<Style x:Key="TextUpperCase" TargetType="{x:Type TextBox}">
<Setter Property="CharacterCasing" Value="Upper"/>
</Style>
<TextBlock
x:Name="ShopNameTextBlock"
TextWrapping="Wrap"
Text="{Binding Description, FallbackValue=Shop name}"
Style="TextUpperCase"
VerticalAlignment="Center"
FontFamily="/GateeClientWPF;component/Fonts/#Letter Gothic L"
FontSize="45"
Grid.ColumnSpan="2"
Margin="0,60,0,0"
FontWeight="Medium"
TextAlignment="Center"
Foreground="Black"
/>
回答by Rohit Vats
CharacterCasingis not valid property for TextBlock, it's for TextBox.
CharacterCasing不是 的有效属性TextBlock,它是为TextBox。
You can have IValueConverterand use it with your binding which will convert text to Upper.
您可以拥有IValueConverter并将其与您的绑定一起使用,这会将文本转换为 Upper。
Declare Converter:
声明转换器:
public class ToUpperValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
if (value is string)
{
return value.ToString().ToUpper();
}
return String.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
return Binding.DoNothing;
}
}
Now, add a reference of your converter in XAML and use like this:
现在,在 XAML 中添加对转换器的引用并使用如下:
<TextBlock Text="{Binding Description,
Converter={StaticResource ToUpperValueConverter}}"/>
回答by Rolando
to use an style, you must first declare in a UserControl.Resources:
要使用样式,您必须首先在 UserControl.Resources 中声明:
<UserControl.Resources>
<Style x:Key="TextUpperCase" TargetType="{x:Type TextBox}">
<Setter Property="CharacterCasing" Value="Upper"/>
</Style>
</UserControl.Resources>

