40

我希望TextBlock, Label,中的所有文本都MenuItem.Header以大写形式显示。字符串取自ResourceDictionary例如:

<TextBlock Text="{StaticResource String1}"/>
<MenuItem Header="{StaticResource MenuItemDoThisAndThat}"/>

等(也用于Label和其他控件)

我不能使用值转换器,因为没有绑定。我不想在字典本身中使字符串大写。

4

8 回答 8

40

我认为这对你有用

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

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

于 2016-01-20T06:33:34.973 回答
37

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

<TextBlock Text="{Binding Source={StaticResource String1},  Converter ={StaticResource myConverter}}"/>
于 2009-11-19T11:32:31.183 回答
33

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

<TextBox CharacterCasing="Upper" Text="{StaticResource String1}" />
于 2011-06-07T16:35:02.257 回答
13

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

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}}"/>
于 2015-04-24T07:35:18.007 回答
7

我为此创建了一个附加属性和转换器。您可能已经拥有转换器,因此请将我对 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);
        }
    }
于 2013-03-25T13:07:22.073 回答
2

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

我相信许多在这里找到自己的方式的人都在寻找如何用一种风格来做到这一点。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。我也不会在其他模板中使用它,而只是严格地作为主题样式。

于 2015-08-14T07:29:54.910 回答
0
    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>
于 2021-06-03T11:56:22.217 回答
-3

您可以使用以下属性将所有输入大小写到 TextBox 控件中:

<TextBox CharacterCasing="Upper"

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

<Style TargetType="{x:Type TextBox}">
  <Setter Property="CharacterCasing" Value="Upper"/>
</Style>
于 2012-08-21T10:14:51.807 回答