您可以創建一個又一個依賴屬性稱爲uppertext和改變依賴屬性的屬性就可以使文本塊爲大寫的文本。請參閱以下代碼。
class UpperTextBlock : TextBlock
{
public string UpperText
{
get { return (string)GetValue(UpperTextProperty); }
set { SetValue(UpperTextProperty, value); }
}
// Using a DependencyProperty as the backing store for UpperText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UpperTextProperty =
DependencyProperty.Register("UpperText", typeof(string), typeof(UpperTextBlock), new PropertyMetadata(string.Empty, OnCurrentReadingChanged));
private static void OnCurrentReadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UpperTextBlock txt = d as UpperTextBlock;
txt.Text = txt.UpperText.ToUpper();
}
}
<local:UpperTextBlock UpperText="test"/>
[WPF/XAML?:如何使所有文字大寫/資本]的
可能重複(http://stackoverflow.com/questions/1762485/wpf-xaml-how-to-make-all- text-upper-case-capital) –
更好的重複http://stackoverflow.com/questions/24956046/wpf-xaml-how-to-make-all-text-upper-case-in-textblock –