Reputation: 309
I get an input from a different view model, and i need to show it in another window without the white spaces inserted. But it should not replace the original text. I need the white spaces removed only when displayed
Upvotes: 1
Views: 709
Reputation: 3834
Here you need Converter
to trim your text like this:
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();
}
}
And use converter with text binding in xaml
like this:
<Windows.Resources>
<converter:NoWhiteSpaceTextConverter x:Key="noWhiteSpaceTextConverter"></converter:NoWhiteSpaceTextConverter>
</Windows.Resources>
<TextBox Text="{Binding YourTextWithSpaces, Converter={StaticResource noWhiteSpaceTextConverter}}" />
Upvotes: 1