WPF:如何将Unicode字符转换为共享资源? [英] WPF: How to shift Unicode character into a shared resource?

查看:53
本文介绍了WPF:如何将Unicode字符转换为共享资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个XAML:

<TextBlock Text="Message with unicode char: &#x24D8;"/>

是否可以通过某种方式将Unicode字符&#x24D8; 转换为共享资源(例如常量或 StaticResource )?

Is there some way to shift that unicode character &#x24D8; into a shared resource (like a constant or a StaticResource)?

这可以正常工作,但是需要有效的绑定才能工作:

This works fine, but it requires a valid binding to work:

<Grid>
    <Grid.Resources>
        <system:String x:Key="ToolTipChar">{0} &#x24D8;</system:String>
    </Grid.Resources>
    <TextBlock Text="{Binding MyText, StringFormat={StaticResource ToolTipChar}}"/>
</Grid>

在后面的代码中:

public string MyText { get; set; } = "Message with unicode char: ";    

方法2

这种方法似乎可行,但没有运气:

Method 2

This method seems like it might work, but no luck:

<Grid>
    <Grid.Resources>
        <system:String x:Key="ToolTipChar">{0} &#x24D8;</system:String>
    </Grid.Resources>
    <TextBlock Text="{Binding Nothing, FallbackValue='Message with unicode char: ', StringFormat={StaticResource ToolTipChar}}"/>
</Grid>

推荐答案

如果我正确理解了您的问题,则应该可以:

If I understand your question correctly, this should work:

<Window.Resources>
    <s:String x:Key="ToolTipChar">{0}&#x24D8;</s:String>
</Window.Resources>
...
<TextBlock Text="{Binding Source='Message with unicode char:', StringFormat={StaticResource ToolTipChar}}" />

这篇关于WPF:如何将Unicode字符转换为共享资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆