如何使所有文本大写/大写?

发布于 2024-08-11 23:52:50 字数 370 浏览 11 评论 0原文

我希望 TextBlockLabelMenuItem.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

森罗 2024-08-18 23:52:50

我认为这对您有用

<TextBlock Text='{StaticResource String1}' Typography.Capitals="AllPetiteCaps"/>

对于字体大写枚举 https://msdn.microsoft.com/en-us/library/system.windows.fontcapitals(v=vs.110).aspx

I think this will work for you

<TextBlock Text='{StaticResource String1}' Typography.Capitals="AllPetiteCaps"/>

For font capitals enumerations https://msdn.microsoft.com/en-us/library/system.windows.fontcapitals(v=vs.110).aspx

初熏 2024-08-18 23:52:50

您仍然可以使用转换器,只需在绑定源中设置文本值:

<TextBlock Text="{Binding Source={StaticResource String1},  Converter ={StaticResource myConverter}}"/>

You still can use a converter, just set the textvalue in the source of the binding :

<TextBlock Text="{Binding Source={StaticResource String1},  Converter ={StaticResource myConverter}}"/>
夏至、离别 2024-08-18 23:52:50

您可以在 TextBox 中使用标签 CharacterCasing,而不是使用转换器,但在您的情况下,它不适用于 TextBlock。

<TextBox CharacterCasing="Upper" Text="{StaticResource String1}" />

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.

<TextBox CharacterCasing="Upper" Text="{StaticResource String1}" />
我纯我任性 2024-08-18 23:52:50

要完成 Peter 的回答(我的编辑已被拒绝),您可以使用如下转换器:

C#:

public class CaseConverter : IValueConverter
{    
    public CharacterCasing Case { get; set; }

    public CaseConverter()
    {
        Case = CharacterCasing.Upper;
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var str = value as string;
        if (str != null)
        {
            switch (Case)
            {
                case CharacterCasing.Lower:
                    return str.ToLower();
                case CharacterCasing.Normal:
                    return str;
                case CharacterCasing.Upper:
                    return str.ToUpper();
                default:
                    return str;
            }
        }
        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

<TextBlock Text="{Binding Source={StaticResource String1}, Converter ={StaticResource myCaseConverter}}"/>

To complete Peter's answer (my edit has been rejected), you can use a converter like this:

C#:

public class CaseConverter : IValueConverter
{    
    public CharacterCasing Case { get; set; }

    public CaseConverter()
    {
        Case = CharacterCasing.Upper;
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var str = value as string;
        if (str != null)
        {
            switch (Case)
            {
                case CharacterCasing.Lower:
                    return str.ToLower();
                case CharacterCasing.Normal:
                    return str;
                case CharacterCasing.Upper:
                    return str.ToUpper();
                default:
                    return str;
            }
        }
        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

<TextBlock Text="{Binding Source={StaticResource String1}, Converter ={StaticResource myCaseConverter}}"/>
孤凫 2024-08-18 23:52:50

我为此创建了一个附加属性和转换器。您可能已经有了转换器,因此请将我对 CaseConverter 的引用替换为您拥有的任何实现。

附加属性只是一个布尔值,如果您希望它是大写的,您可以设置它(显然您可以将其扩展为选择样式的可枚举值)。当属性更改时,它会根据需要重新绑定 TextBlock 的 Text 属性,并添加转换器。

当属性已经绑定时,可能需要做更多的工作 - 我的解决方案假设它是一个简单的路径绑定。但它可能还需要复制源等。但是我觉得这个例子足以表达我的观点。

这是附加的属性:

public static bool GetUppercase(DependencyObject obj)
    {
        return (bool)obj.GetValue(UppercaseProperty);
    }

    public static void SetUppercase(DependencyObject obj, bool value)
    {
        obj.SetValue(UppercaseProperty, value);
    }

    // Using a DependencyProperty as the backing store for Uppercase.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UppercaseProperty =
        DependencyProperty.RegisterAttached("Uppercase", typeof(bool), typeof(TextHelper), new PropertyMetadata(false, OnUppercaseChanged));

    private static void OnUppercaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock txt = d as TextBlock;

        if (txt == null) return;

        var val = (bool)e.NewValue;

        if (val)
        {
            // rebind the text using converter
            // if already bound, use it as source

            var original = txt.GetBindingExpression(TextBlock.TextProperty);

            var b = new Binding();

            if (original != null)
            {
                b.Path = original.ParentBinding.Path;
            }
            else
            {
                b.Source = txt.Text;
            }

            b.Converter = new CaseConverter() { Case = CharacterCasing.Upper };


            txt.SetBinding(TextBlock.TextProperty, b);
        }
    }

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:

public static bool GetUppercase(DependencyObject obj)
    {
        return (bool)obj.GetValue(UppercaseProperty);
    }

    public static void SetUppercase(DependencyObject obj, bool value)
    {
        obj.SetValue(UppercaseProperty, value);
    }

    // Using a DependencyProperty as the backing store for Uppercase.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UppercaseProperty =
        DependencyProperty.RegisterAttached("Uppercase", typeof(bool), typeof(TextHelper), new PropertyMetadata(false, OnUppercaseChanged));

    private static void OnUppercaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock txt = d as TextBlock;

        if (txt == null) return;

        var val = (bool)e.NewValue;

        if (val)
        {
            // rebind the text using converter
            // if already bound, use it as source

            var original = txt.GetBindingExpression(TextBlock.TextProperty);

            var b = new Binding();

            if (original != null)
            {
                b.Path = original.ParentBinding.Path;
            }
            else
            {
                b.Source = txt.Text;
            }

            b.Converter = new CaseConverter() { Case = CharacterCasing.Upper };


            txt.SetBinding(TextBlock.TextProperty, b);
        }
    }
梦回梦里 2024-08-18 23:52:50

这并没有严格回答问题,但确实提供了产生相同效果的技巧。

我相信许多人在这里找到了自己的路,正在寻找如何以一种风格来做到这一点。 TextBlock 在这里有点棘手,因为它不是 Control,而是 FrameworkElement,因此您无法定义 Template 来完成此任务。

最有可能需要使用全部大写文本的标题或类似的内容,其中标签的使用是合理的。我的解决方案是:

<!-- Examples of CaseConverter can be found in other answers -->

<ControlTemplate x:Key="UppercaseLabelTemplate" TargetType="{x:Type Label}">
    <TextBlock Text="{TemplateBinding Content, Converter={StaticResource CaseConverter}}" />
</ControlTemplate>

<Style x:Key="UppercaseHeadingStyle"
       TargetType="{x:Type Label}">
    <Setter Property="FontSize" Value="20" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Template" Value="{StaticResource UppercaseLabelTemplate}" />
</Style>

<!-- Usage: -->
<Label Content="Header" Style="{StaticResource UppercaseHeadingStyle}" />

请注意,这确实禁用了 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:

<!-- Examples of CaseConverter can be found in other answers -->

<ControlTemplate x:Key="UppercaseLabelTemplate" TargetType="{x:Type Label}">
    <TextBlock Text="{TemplateBinding Content, Converter={StaticResource CaseConverter}}" />
</ControlTemplate>

<Style x:Key="UppercaseHeadingStyle"
       TargetType="{x:Type Label}">
    <Setter Property="FontSize" Value="20" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Template" Value="{StaticResource UppercaseLabelTemplate}" />
</Style>

<!-- Usage: -->
<Label Content="Header" Style="{StaticResource UppercaseHeadingStyle}" />

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.

停顿的约定 2024-08-18 23:52:50
    public class StaticResourceToUpperExtension : StaticResourceExtension
    {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var resource = base.ProvideValue(serviceProvider);
            if (resource is string str)
                return str.ToUpper();
            return resource;
        }

        public StaticResourceToUpperExtension() : base() { }
        public StaticResourceToUpperExtension(object resourceKey) : base(resourceKey) { }

    }
    <Grid>
        <FrameworkElement.Resources>
            <sys:String x:Key="String1">any text</sys:String>
        </FrameworkElement.Resources>
        <TextBlock Text="{local:StaticResourceToUpper String1}"/>
    </Grid>
    public class StaticResourceToUpperExtension : StaticResourceExtension
    {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var resource = base.ProvideValue(serviceProvider);
            if (resource is string str)
                return str.ToUpper();
            return resource;
        }

        public StaticResourceToUpperExtension() : base() { }
        public StaticResourceToUpperExtension(object resourceKey) : base(resourceKey) { }

    }
    <Grid>
        <FrameworkElement.Resources>
            <sys:String x:Key="String1">any text</sys:String>
        </FrameworkElement.Resources>
        <TextBlock Text="{local:StaticResourceToUpper String1}"/>
    </Grid>
辞取 2024-08-18 23:52:50

您可以使用以下属性对 TextBox 控件中的所有输入进行大小写:

<TextBox CharacterCasing="Upper"

要应用于整个应用程序中的所有 TextBox 控件,请为所有 TextBox 控件创建一个样式:

<Style TargetType="{x:Type TextBox}">
  <Setter Property="CharacterCasing" Value="Upper"/>
</Style>

You can case all input into TextBox controls with the following property:

<TextBox CharacterCasing="Upper"

To apply to all TextBox controls in the entire application create a style for all TextBox controls:

<Style TargetType="{x:Type TextBox}">
  <Setter Property="CharacterCasing" Value="Upper"/>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文