Silverlight 4 Text Box Does Not Display All Content

Sometimes, when I use a text box, it is not possible to scroll all the way down to see the last words.

I included an example with three text fields with the same content, but with different widths. All words are not displayed on the left.

Scroll to the end of the text field by selecting it, and then press 'ctrl' + 'end'. When I do this for the text field on the left ('_tb1'), I do not see the cursor, and I do not see the last words. It seems that the cursor and words are "below" the text box. I can mark and copy text that is not displayed. The last word should be "si +", see Code below. I checked that the Text property of the text field contains all the text.

This so far only happened when I used "TextWrapping =" Wrap "and for a specific width.

Any suggestions for fixing it?

Textbox to the left does not show last words

<UserControl x:Class="Silverlight4TextBoxProblem.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Width="500" Height="150"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBox Name="_tb1" Grid.Column="0" TextWrapping="Wrap" FontFamily="Arial" FontSize="12" VerticalScrollBarVisibility="Visible" Width="100"/> <TextBox Name="_tb2" Grid.Column="1" TextWrapping="Wrap" FontFamily="Arial" FontSize="12" VerticalScrollBarVisibility="Visible" Width="75"/> <TextBox Name="_tb3" Grid.Column="2" TextWrapping="Wrap" FontFamily="Arial" FontSize="12" VerticalScrollBarVisibility="Visible" Width="150"/> <Button Grid.Column="3" Click="ButtonClick" Content="Assert _tb1"/> </Grid> </UserControl> 

Code for

 public partial class MainPage : UserControl { private readonly string ErrorText = @"Lorem ipsum dolor sit amet+++," + Environment.NewLine + "consectetur adipisicing elit, sed do eiusmod" + Environment.NewLine + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam," + Environment.NewLine + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo" + Environment.NewLine + "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse" + Environment.NewLine + "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non" + Environment.NewLine + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem" + Environment.NewLine + "ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor" + Environment.NewLine + "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis" + Environment.NewLine + "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." + Environment.NewLine + "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu" + Environment.NewLine + "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in" + Environment.NewLine + "culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit" + Environment.NewLine + "amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore" + Environment.NewLine + "et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation" + Environment.NewLine + "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor" + Environment.NewLine + "in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla" + Environment.NewLine + "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui" + Environment.NewLine + "officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet," + Environment.NewLine + "consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et" + Environment.NewLine + "dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco" + Environment.NewLine + "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in" + Environment.NewLine + "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." + Environment.NewLine + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia" + Environment.NewLine + "deserunt mollit anim id est laborum. Lorem ipsum dolor si+"; public MainPage() { InitializeComponent(); _tb1.Text = ErrorText; _tb2.Text = ErrorText; _tb3.Text = ErrorText; } private void ButtonClick(object sender, RoutedEventArgs e) { MessageBox.Show(_tb1.Text.Last().ToString()); } } 
+4
source share
2 answers

So, I had the same sign. Depending on your requirements, this can be resolved using RichTextBox instead. And as a bonus, it seems that you get faster scrolling.

However, RTB does not have a text property or a max length property for binding. I solved this by inheriting RTB for my CustomTextBox.

If you are connecting to a contentchanged event

 public class CustomTextBox : RichTextBox { public CustomTextBox() { ContentChanged += HandleContentChanged; } ... 

and then

 private void HandleContentChanged(object sender, ContentChangedEventArgs e) { var text = GetContent(out blocks); Text = text; } 

and add TextProperty to bind to

 public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(CustomTextBox), new PropertyMetadata(string.Empty, HandleTextChanged)); private static void HandleTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var customTextBox = (CustomTextBox)d; var newValue = (string)e.NewValue; //This method is only run when set from codebehind UpdateOnTextChange(customTextBox, newValue); } private static void UpdateOnTextChange(CustomTextBox customTextBox, string newValue) { int blocks; if (customTextBox.GetContent(out blocks) != newValue) { var binding = customTextBox.GetBindingExpression(TextProperty); customTextBox.Blocks.Clear(); var value = EnsureString(newValue); customTextBox.Selection.Text = value; if (customTextBox.HasTextChanges) { customTextBox.HasTextChanges = false; //reset binding... if (binding != null) { customTextBox.SetBinding(TextProperty, binding.ParentBinding); } } var bindingExpression = customTextBox.GetBindingExpression(TextProperty); if (bindingExpression != null) bindingExpression.UpdateSource(); } } 

And since you want to get the contents of CustomTextBox as a string:

  public string GetContent(out int blocks) { var builder = new StringBuilder(); blocks = 0; foreach (var run in Blocks.OfType<Paragraph>().Select(paragraph => paragraph.Inlines.FirstOrDefault()).OfType<Run>()) { blocks++; builder.Append(run.Text); builder.Append(Environment.NewLine); } var content = builder.ToString(); if (content.EndsWith(Environment.NewLine, StringComparison.CurrentCulture)) content = content.Substring(0, content.Length - 2); return content ?? string.Empty; } 
+1
source

Wow, this is really a mistake! The problem seems to be related to the internal control of the TextBoxView. It does not correctly calculate text for some situations. You can see the actual source of the problem with Silverlight Spy.

I tried to find a solution to the problem, but could not find it. I tried to mess with the template, and nothing works in 100% of cases.

We must report this issue to Microsoft.

+1
source

Source: https://habr.com/ru/post/1336771/


All Articles