【问题标题】:Display special characters in WPF TextBox在 WPF 文本框中显示特殊字符
【发布时间】:2019-12-20 15:58:25
【问题描述】:

我有一个类似[VIP] 的字符串,但文本框只显示为[VIP]。我怎样才能像在浏览器中一样将这个特殊字符至少显示为正方形?尝试设置多个字体系列(<Setter Property="FontFamily" Value="Arial, Symbol"/>),但不起作用。 我不想使用richtextbox,因为它会大大增加窗口渲染时间。

upd: 好吧,stackoverflow 文本渲染器也吃掉了这个字符,所以字符串是 "\u0001[\u0004VIP\u0001] "

【问题讨论】:

  • 我认为的解决方案是创建一个派生 TextBox 的自定义控件并覆盖 PreviewKeyDown 方法。然后再之后,在方法中将输入字符串的字符convert(或)字符转换成你想要的字符。
  • 或者创建附加属性,将输入字符串的[或]字符转换为你想要的字符。

标签: c# wpf xaml textbox wpf-controls


【解决方案1】:

我希望我能很好地理解你的问题。

将以下附加属性添加到您的项目中。它可以将“[”字符转换为“\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
}

通过这种方式,您可以添加您想要的功能。

上面的代码可以在主项目中使用,如下所示。

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

【讨论】:

  • 我想到了转换器,我想我会做的,谢谢。但是另一个sybmols呢?如何理解将打印哪个符号,哪个不打印?
  • 我认为这是另一个“主题”,所以也许您应该搜索该主题。
猜你喜欢
  • 2019-08-09
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多