WPF: How to transfer a Unicode character to a share?
I have this XAML:
<TextBlock Text="Message with unicode char: ⓘ"/>
Is there a way to shift the unicode character ⓘ
to a shared resource (e.g. constant or StaticResource
)?
What i tried
Method 1
This works fine, but a valid commitment is required to work:
<Grid>
<Grid.Resources>
<system:String x:Key="ToolTipChar">{0} ⓘ</system:String>
</Grid.Resources>
<TextBlock Text="{Binding MyText, StringFormat={StaticResource ToolTipChar}}"/>
</Grid>
And in the code behind:
public string MyText { get; set; } = "Message with unicode char: ";
Method 2
This method seems to work, but no luck:
<Grid>
<Grid.Resources>
<system:String x:Key="ToolTipChar">{0} ⓘ</system:String>
</Grid.Resources>
<TextBlock Text="{Binding Nothing, FallbackValue='Message with unicode char: ', StringFormat={StaticResource ToolTipChar}}"/>
</Grid>
This will also work:
<Window.Resources>
<system:String x:Key="ToolTipChar">ⓘ</system:String>
</Window.Resources>
...
<TextBlock Text="{Binding StringFormat='Message with unicode char: {0}',
Source={StaticResource ToolTipChar}}" />
I believe this is a little readable and easier to understand than putting a replacement token directly in a resource string
.
, , TextBlock.Inlines
<my:String x:Key="TooltipSign">ⓘ</my:String>
<TextBlock HorizontalAlignment="Right" Margin="10">
<Run Text="Message with unicode char:"/>
<Run Text="{StaticResource TooltipSign}"
FontWeight="Bold" Foreground="Orange" Background="Black"/>
</TextBlock>
TextBlock.Inlines
- TextBlock, <TextBlock.Inlines>
. Inlines , :