如何簡單附加屬性來監視TextBlock
的Text
屬性的更改?
public static class Helper
{
public static bool GetUseUpperCase(DependencyObject obj)
{
return (bool)obj.GetValue(UseUpperCaseProperty);
}
public static void SetUseUpperCase(DependencyObject obj, bool value)
{
obj.SetValue(UseUpperCaseProperty, value);
}
public static readonly DependencyProperty UseUpperCaseProperty =
DependencyProperty.RegisterAttached("UseUpperCase", typeof(bool), typeof(TextBlock), new PropertyMetadata(false, (sender, args) =>
{
var textBlock = (TextBlock)sender;
textBlock.RegisterPropertyChangedCallback(TextBlock.TextProperty, (s, e) =>
{
textBlock.Text = textBlock.Text.ToUpper();
});
}));
}
然後你只需將其連接到TextBlock
需要大寫。
<TextBlock x:Uid="SettingsPage_StreamQualityTextBlock"
Style="{StaticResource SectionTitleStyle}"
local:Helper.UseUpperCase="True" />
工作就像一個魅力,非常感謝! –