Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.06 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Ignoring while preserving line breaks in WPF TextBox
  2. <Grid Background="AliceBlue">
  3.   <Grid.RowDefinitions>
  4.      <RowDefinition Height="100"/>
  5.      <RowDefinition Height="100"/>
  6.   </Grid.RowDefinitions>
  7.  
  8.   <TextBox x:Name="FirstTextBox"
  9.      AcceptsReturn="True"
  10.      TextWrapping="Wrap"
  11.      Text="{Binding MyString}"
  12.      Width="150"/>
  13.  
  14.   <TextBox x:Name="SecondTextBox"
  15.      Grid.Row="1"
  16.      AcceptsReturn="False"
  17.      TextWrapping="NoWrap"
  18.      Text="{Binding MyString}"
  19.      VerticalAlignment="Top"/>
  20. </Grid>
  21.        
  22. public class IgnoreLinebreaksConverter : IValueConverter
  23. {
  24.     private const string Sub = " u200B";
  25.     private const string Lb = "rn";
  26.  
  27.     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  28.     {
  29.         var s = (string)value;
  30.         return string.IsNullOrEmpty(s) ? s : Regex.Replace(s, Lb, Sub);
  31.     }
  32.  
  33.     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  34.     {
  35.         var s = (string)value;
  36.         return string.IsNullOrEmpty(s) ? s : Regex.Replace(s, Sub, Lb);
  37.     }
  38. }