I have a string like [VIP]
, but textbox only shows it as [VIP]
. How can i display this special characters atleast as squares like in browser? Tried to set multiple font families(<Setter Property="FontFamily" Value="Arial, Symbol"/>
), but it does not work.
I dont wanna use richtextbox because its hugely increase window rendering time.
upd: well, stackoverflow text renderer eating this characters too, so the string is
"\u0001[\u0004VIP\u0001] "
I hope I understood your question well.
Add the attached property of the below to your project. it could converts "[" character to the "\u0001".
class CharacterConvertBehavior : DependencyObject
{
public static bool GetConvertEnable(DependencyObject obj)
{
return (bool)obj.GetValue(ConvertEnableProperty);
}
public static void SetConvertEnable(DependencyObject obj, bool value)
{
obj.SetValue(ConvertEnableProperty, value);
}
// Using a DependencyProperty as the backing store for ConvertEnable. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ConvertEnableProperty =
DependencyProperty.RegisterAttached("ConvertEnable", typeof(bool), typeof(CharacterConvertBehavior), new PropertyMetadata(ConvertEnableChanged));
private static void ConvertEnableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textBox = d as TextBox;
if ((bool)e.NewValue == true)
textBox.PreviewKeyDown += TextBox_ConvertHandler;
else
textBox.PreviewKeyDown -= TextBox_ConvertHandler;
}
#region Convert Handler
private static void TextBox_ConvertHandler(object sender, KeyEventArgs e)
{
var textBox = sender as TextBox;
if (e.Key == Key.Oem4) // "["
{
string convertString = "\\u0001";
TextCompositionManager.StartComposition(new TextComposition(InputManager.Current, textBox, convertString));
e.Handled = true;
}
}
#endregion
}
In this fashion, you could add the feature you want.
The above code could be used in the main project as shown below.
<Window x:Class="StackOverFlowAnswers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:attached="clr-namespace:Parse.WpfControls.AttachedProperties"
xmlns:local="clr-namespace:StackOverFlowAnswers"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="ConvertableTextBox" TargetType="TextBox">
<Setter Property="attached:CharacterConvertBehavior.ConvertEnable" Value="True"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Style="{StaticResource ConvertableTextBox}"/>
</Grid>
</Window>