我从其他视图模型获得输入,并且需要在没有插入空白的另一个窗口中显示它。但它不应替换原始文本。我需要仅在显示时删除空白
答案 0 :(得分:1)
在这里您需要Converter
来像这样修剪文本:
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
[ValueConversion( typeof( string ), typeof( string ) )]
public class NoWhiteSpaceTextConverter : IValueConverter
{
public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
{
if ( ( value is string ) == false )
{
throw new ArgumentNullException( "value should be string type" );
}
string returnValue = ( value as string );
return returnValue != null ? returnValue.Trim() : returnValue;
}
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
{
throw new NotImplementedException();
}
}
像这样在xaml
中使用带有文本绑定的转换器:
<Windows.Resources>
<converter:NoWhiteSpaceTextConverter x:Key="noWhiteSpaceTextConverter"></converter:NoWhiteSpaceTextConverter>
</Windows.Resources>
<TextBox Text="{Binding YourTextWithSpaces, Converter={StaticResource noWhiteSpaceTextConverter}}" />