如何通过 XAML 在 WPF 中将小写转换为大写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13990468/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:43:46  来源:igfitidea点击:

How to convert lower case to upper case in WPF by XAML?

c#wpfxamltextboxlowercase

提问by Chandru A

I tried to convert upper case to lower case by XAML in WPF like below:

我尝试在 WPF 中通过 XAML 将大写转换为小写,如下所示:

<TextBox Height="86" CharacterCasing="Upper"/>

I want to achieve the same scenario with TextBlock, Labeland Button.

我想用TextBlock,Label和实现相同的场景Button

How can I do it?

我该怎么做?

回答by David Blurton

You should use a value converter:

您应该使用值转换器:

public class ToUpperValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var str = value as string;
        return string.IsNullOrEmpty(str) ? string.Empty : str.ToUpper();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

回答by Ramin

One way is to do this is to use NotifyOnTargetUpdatedand handle TextChangedevent.

一种方法是使用NotifyOnTargetUpdated和处理TextChanged事件。

XAML

XAML

<TextBlock Name="TB" Text="{Binding Path=YourProperty, NotifyOnTargetUpdated=True}"
               TargetUpdated="TB_TargetUpdated" />

Code behind

背后的代码

private void TB_TargetUpdated(object sender, DataTransferEventArgs e)
{
   TB.Text = TB.Text.ToUpper();
}

回答by Qortex

Just take a look at that: How to make all text upper case / capital?.

看看那个:如何使所有文本大写/大写?.

More generally, each time you want to transform a value to go into a control, think of a converter and write it yourself (or use it if it already exists).

更一般地说,每次你想把一个值转换成一个控件时,考虑一个转换器并自己编写它(或者如果它已经存在就使用它)。

You can find additional documentation on converters here: http://wpftutorial.net/ValueConverters.html.

您可以在此处找到有关转换器的其他文档:http: //wpftutorial.net/ValueConverters.html