I would snap TextBoxto ValueConverter, which removes spaces on demand and replaces them with underscores.
ValueConverter will look something like this:
public class SpaceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Convert.ToString(value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string text = System.Convert.ToString(value);
text = text.Replace(" ", "_");
return text;
}
}
And yours TextBoxwill be attached to it as follows:
<TextBox Text="{Binding Path=UserString, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource SpaceConverter}}" />
Note what UserStringshould be in the current DataContext.
SpaceConverter XAML. , , UserControl, :
<UserControl.Resources>
<local:SpaceConverter x:Key="SpaceConverter" />
</UserControl.Resources>
local , SpaceConverter.cs.