将文本框中的空格显示为不同的字符 [英] Display whitespace in a textbox as a different character
问题描述
如何显示在 System.Windows.Control.TextBox
中输入的所有空格字符作为其他字符,如点? · · · ·
例如:
输入您的文字:[abc··· ·· def]
理想情况下,我也希望以比其他文本更浅的颜色显示点
当用户输入空格时,文本框应该只显示其他的东西。理想情况下,我不想修改实际的字符串。因此, TextBox.Text
的内部值应该保持不变,当用户将内容复制到剪贴板,或者以类似的方式与文本框交互时,它们应该得到空格而不是点。
我在相关的 Stackoverflow问题,覆盖OnTextChangedEvent或使用IValueConverter,可以替换输入的空格,但这当然也会修改支持字符串和我希望这是完全透明的应用程序和用户。
你可以做一个值转换器 - 东西像这样为我工作: 我还在文本框的命令绑定对于 给我这样的XAML: 注意:emptor:如果用户输入一个点文本框,如果转换器运行,也会导致从后台字符串丢失该字符。 How can I display all space characters entered in a e.g.: Enter your text: [abc······def] Ideally I would also want to display the dot in a lighter color than the rest of the text When the user enters whitespace, the textbox should only display something else. Ideally I do not want to modify the actual string. So the internal value of The solutions I found in a related Stackoverflow question, overriding the OnTextChangedEvent or using an IValueConverter, can replace the the spaces as they are entered, but this will of course also modify the backing string and I would prefer this to be totally transparent to the application and the user. You can do this with a value converter - something like this worked for me: I also added the following to the text box's command binding for Giving me XAML like this: Caveat emptor: this will end up translating the dot character to a space if the user enters one into the textbox, and would also result in losing that character from the backing string if the converter ran. 这篇关于将文本框中的空格显示为不同的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ 公共密封的类WhiteSpaceConverter:IValueConverter
{
公共对象Convert(object值,类型targetType,对象参数,CultureInfo文化)
{
st ring str =(string)value;
if(str == null)返回值;
return str.Replace(,·);
$ b公共对象ConvertBack(对象值,类型targetType,对象参数,CultureInfo文化)
{
string str =(string)value;
if(str == null)返回值;
return str.Replace(·,);
$ b $ p
$ b ApplicationCommands.Copy
(所以如果用户选择并复制空白,他们将得到acutal空间而不是点字符替换它):
static Converters.WhiteSpaceConverter conv = new Converters.WhiteSpaceConverter();
private void TextBox_Copy_Executed(object sender,ExecutedRoutedEventArgs e)
{
TextBox tb = sender as TextBox;
$ b $ if(tb!= null)
Clipboard.SetText((string)conv.ConvertBack(tb.SelectedText,typeof(TextBox),null,System.Globalization.CultureInfo.CurrentCulture)) ;
$ / code>
< cnv:WhiteSpaceConverter x:Key =WhiteSpaceConverter/>
...
Mode = TwoWay,
UpdateSourceTrigger = PropertyChanged,
Converter = {StaticResource WhiteSpaceConverter}}
>
< TextBox.CommandBindings>
< CommandBinding Command ={x:Static ApplicationCommands.Copy}Executed =TextBox_Copy_Executed/>
< /TextBox.CommandBindings>
< / TextBox>
System.Windows.Control.TextBox
as some other character, such as a dot? · · · ·
TextBox.Text
should remain unchanged, and when the user copies the content to the clipboard, or interacts in a similar way with the textbox, they should get spaces and not dots.public sealed class WhiteSpaceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string str = (string)value;
if (str == null) return value;
return str.Replace(" ", "·");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string str = (string)value;
if (str == null) return value;
return str.Replace("·", " ");
}
}
ApplicationCommands.Copy
(so if the user selects and copies the whitespace, they get the acutal space instead of the dot character replacing it):static Converters.WhiteSpaceConverter conv = new Converters.WhiteSpaceConverter();
private void TextBox_Copy_Executed(object sender, ExecutedRoutedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null)
Clipboard.SetText((string)conv.ConvertBack(tb.SelectedText, typeof(TextBox), null, System.Globalization.CultureInfo.CurrentCulture));
}
<cnv:WhiteSpaceConverter x:Key="WhiteSpaceConverter" />
...
<TextBox FontFamily="Consolas"
Text="{Binding ViewModelStringProperty,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource WhiteSpaceConverter}}"
>
<TextBox.CommandBindings>
<CommandBinding Command="{x:Static ApplicationCommands.Copy}" Executed="TextBox_Copy_Executed" />
</TextBox.CommandBindings>
</TextBox>