我的页面上有这个简单的 TextBlock:
<TextBlock x:Uid="SettingsPage_StreamQualityTextBlock"
Style="{StaticResource SectionTitleStyle}"/>
Text
值由 x:Uid
指令从资源文件设置。
问题是我想通过 StaticResource
将自定义转换器应用于 TextBlock
的 Text
值,但是当我这样做时
Text="{Binding Converter={StaticResource TextToUpperCase}}"
未设置值且未应用转换器。
因此我的问题是:
这是否可以在 XAML 中以某种方式在不对代码后面的部分进行编程修改的情况下完成?
提前感谢您的帮助!
最佳答案
监控 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" />
关于c# - UWP - 将自定义转换器应用于 x :Uid directive 设置的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44739001/