Search code examples
c#.netwpftextblock

TextBlock Run Background Span Whole Line


Is it possible to make a run span the entire width of a TextBlock so the background looks natural and not just on the text of the run?

Current Result: The Background only spans the length of the text.

Expected Result: The Background spans the entire textbox.

Another issue with the first image is that there are (unwanted) white lines between certain Runs (in this case "test" and "1"). My current code is:

Run r = new Run("You: " + SendMessageBox.Text + "\n");
r.Background = Brushes.LightBlue;
ChatHistory.Inlines.Add(r);

Solution

  • Is it possible to make a run span the entire width of a TextBlock so the background looks natural and not just on the text of the run?

    No, you should add another TextBlock element for each line:

    TextBlock t = new TextBlock() { Text = "You: " + SendMessageBox.Text };
    t.Background = Brushes.LightBlue;
    ChatHistory.Children.Add(t);
    

    ChatHistory could for example be a StackPanel: