0

I am having a bit of a trouble when using TextBox in combination with an oblique text.

I took a look at a similar SO question where the control used is actually a TextBlock and increasing the Height (as suggested) neither solves the problem for TextBox nor fits the needs of my scenario as I need the Height to be set implicitly based on the Height of the text inside the TextBox.

Apart from that, I was not able to find any other related questions.

I have the following XAML code:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <TextBox
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        FontStyle="Normal"
        FontSize="50"
        Text="Another text">
    </TextBox>
</Window>

Everything looks fine until I change the FontStyle to Oblique (click on the links below to see the pictures):

FontStyle="Normal"

FontStyle="Oblique"

I found out the problem is probably caused by TextBoxView inside the TextBox which has its Width computed in a rather strange way. It seems that a portion of letter reaches outside of its "selection bounds" (when you take a look at the selected text and the Snoop WPF element highlight I am sure you will know what I mean).

Selected text

Snoop WPF element highlight

As it turns out, adjusting the TextBoxView Width in Snoop WPF resolves the problem. However, I am not able to achieve the same in XAML.

I could probably work this around by creating a custom template for TextBox, but it certainly seems like an overkill and an awful amount of work for something I would expect to be working properly in the first place.

Did I miss something or is it really so difficult to get it working the right way?

PS: I really need the text to be selectable so using a TextBlock instead of TextBox is out of the question.

dva
  • 303
  • 1
  • 2
  • 12
  • Have you tried Padding="0,0,10,0"? – Kevin Cook Apr 04 '19 at 12:44
  • Yes, I have. The problem is, that the `Padding` will make the whole TextBox wider, but the red-bordered area (from this [picture](https://i.stack.imgur.com/5Da0z.png)) will remain the same. As a result, there will be an extra white area in the TextBox next to the text (or, if you will, the TextBoxView) but the text will be still clipped. – dva Apr 04 '19 at 14:45
  • Have you tried setting a max-width? – M.B. Apr 05 '19 at 12:48
  • Not before you mentioned it but then I gave it a try, although I was not sure how is that supposed to help since I actually want it to be _wider_ (so a `MinWidth` would make much more sense in this case). Setting `MaxWidth` indeed did not solve the problem. `MinWidth` helps, if set to the right value, but so can `Width` itself. The question is what is the right value. I need it to be set according to the space taken up by the text which, obviously, differs throughout the usages of TextBox. – dva Apr 08 '19 at 07:02

0 Answers0