c# - UWP - 将自定义转换器应用于 x :Uid directive 设置的值

标签 c# wpf xaml uwp

我的页面上有这个简单的 TextBlock:

<TextBlock x:Uid="SettingsPage_StreamQualityTextBlock"
           Style="{StaticResource SectionTitleStyle}"/>

Text 值由 x:Uid 指令从资源文件设置。

问题是我想通过 StaticResource 将自定义转换器应用于 TextBlockText 值,但是当我这样做时

Text="{Binding Converter={StaticResource TextToUpperCase}}"

未设置值且未应用转换器。

因此我的问题是:

这是否可以在 XAML 中以某种方式在不对代码后面的部分进行编程修改的情况下完成?

提前感谢您的帮助!

最佳答案

监控 TextBlockText 属性变化的简单附加属性如何?

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/

相关文章:

c# - 类库中的可移植 XAML 样式

xaml - FlowDocument 中同一行上的两个 block

xaml - 实现 ListView 的 float 操作按钮bottom_right - Xamarin.forms

c# - 如何在 asp.net mvc 中模拟基本 Controller ?

c# - WPF 图像转换

c# - Open Emitted 程序集生成的代码在 Reflector 中显示为空,而实际上却不是。

.net - WPF:使用 JPEG 压缩的 TIFF 图像?

c# - Visual Studio : Unable to add to the Web site. ..无法添加文件...访问被拒绝错误 550

c# - 更快地插入sqlite3处理