Guest User

Untitled

a guest
May 2nd, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment