c# - WPF:如何将 Unicode 字符转移到共享资源中?

标签 c# wpf

我有这个 XAML:

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

有什么方法可以将 unicode 字符 转换为共享资源(如常量或 StaticResource)?

我尝试过的

方法一

这工作正常,但它需要一个有效的绑定(bind)才能工作:

<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: ";    

方法二

这个方法似乎可行,但运气不好:

<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>

最佳答案

如果我正确理解你的问题,这应该有效:

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

关于c# - WPF:如何将 Unicode 字符转移到共享资源中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47754636/

相关文章:

c# - WPF:一个窗口,多个 View

c# - 客户端 - 服务器 WebSocket 握手

c# - XDocument + IEnumerable 导致 System.Xml.Linq.dll 内存不足异常

c# - CSS 中的 JavaScript 在回发时丢失 (ASP.NET)

c# - 错误 CS7069 对类型 'Image' 的引用声称它是在 'System.Drawing' 中定义的,但找不到

wpf - WPF 中的数据绑定(bind)现象(绑定(bind)到 FrameworkElement) - 有什么想法吗?

c# - 如何从 C# 列表中检索常用项

wpf - 空白 - 如果图像源是绝对 Uri,则 WPF 中的黑色图像控件

.net - Awesomium WebControl 挂起

c# - 如何将图片合并到 WriteableBitmap 中?