2014-07-25 67 views
7

我想顯示在大寫WPF/XAML:如何使TextBlock中的所有文本大寫?

<TextBlock Name="tbAbc" 
      FontSize="12" 
      TextAlignment="Center" 
      Text="Channel Name" 
      Foreground="{DynamicResource {x:Static r:RibbonSkinResources.RibbonGroupLabelFontColorBrushKey}}" /> 

在TextBlock中的所有字符的字符串可以通過綁定拍攝。我不想在字典中使字符串大寫。

+0

見[C#字符串格式標誌或修飾符小寫param](http://stackoverflow.com/questions/1839649/c-sharp-string-format-flag-or-modifier-to-lowercase-param) – pushpraj

+0

[WPF/XAML的可能重複:如何使所有文本大寫/大寫?](http://stackoverflow.com/questions/1762485/wpf- xaml-how-to-make-all-text-upper-case-capital) –

回答

10

執行自定義轉換器。

using System.Globalization; 
using System.Windows.Data; 
// ... 
public class StringToUpperConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value != null && value is string) 
     { 
      return ((string)value).ToUpper(); 
     } 

     return value; 
    } 

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

然後包括在您的XAML作爲一種資源:

<local:StringToUpperConverter x:Key="StringToUpperConverter"/> 

並把它添加到你的綁定:

Converter={StaticResource StringToUpperConverter} 
2

如果不是的話,你可以使用文本框大不了而不是TextBlock是這樣的:

<TextBox CharacterCasing="Upper" IsReadOnly="True" /> 
+0

@ user3876923 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – RazvanR

+0

這不是真的這是一個可以接受的答案,因爲這個問題是關於TextBlock的。 –

+1

@StephenDrew @StephenDrew我是**不**要求他接受我的回答,讓他知道他應該接受一個(最能幫助他的人 - 這就是關於這個網站的所有內容,對吧?)。在我看來,轉換器是一個很好的答案。 (我的只是一個選擇)。 – RazvanR

1

我使用的字符外殼值轉換器:

class CharacterCasingConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var s = value as string; 
     if (s == null) 
      return value; 

     CharacterCasing casing; 
     if (!Enum.TryParse(parameter as string, out casing)) 
      casing = CharacterCasing.Upper; 

     switch (casing) 
     { 
      case CharacterCasing.Lower: 
       return s.ToLower(culture); 
      case CharacterCasing.Upper: 
       return s.ToUpper(culture); 
      default: 
       return s; 
     } 
    } 

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 
15

或者在你的TextBlock定義中使用

Typography.Capitals="AllSmallCaps" 

在這裏看到:MSDN - Typography.Capitals

編輯:

這不的Windows Phone 8.1工作,只能在Windows 8.1 ...

+2

這不起作用,首都將是大寫字母,小寫字母將是小寫字母。 – Wouter

相關問題