
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.06 KB | hits: 17 | expires: Never
Ignoring while preserving line breaks in WPF TextBox
<Grid Background="AliceBlue">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<TextBox x:Name="FirstTextBox"
AcceptsReturn="True"
TextWrapping="Wrap"
Text="{Binding MyString}"
Width="150"/>
<TextBox x:Name="SecondTextBox"
Grid.Row="1"
AcceptsReturn="False"
TextWrapping="NoWrap"
Text="{Binding MyString}"
VerticalAlignment="Top"/>
</Grid>
public class IgnoreLinebreaksConverter : IValueConverter
{
private const string Sub = " u200B";
private const string Lb = "rn";
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = (string)value;
return string.IsNullOrEmpty(s) ? s : Regex.Replace(s, Lb, Sub);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = (string)value;
return string.IsNullOrEmpty(s) ? s : Regex.Replace(s, Sub, Lb);
}
}