October 22, 2024
Chicago 12, Melborne City, USA
templates

WPF Custom TextBox Sizing issue


I am trying to create a custom TextBox Template in WPF.

My TextBox has a 1px border around it, this border has a margin of 8px, this is all simple stuff, no issues here.

The issue I have is that when I use my TextBox and define:

<TextBox Template="MyTextBoxTemplate" Height="100">

Height set at 100, output height 84

The actual output height of the border is only 84px, I guess because when I set the height, that also includes the top and bottom Margins (2 x 8px) 16px – but I say the TextBox should be 100px high, that is what I want to be output.

I have tried within my template to set the height of the border based on {TemplateBinding Height} but then the bottom of the border is trimmed:

Border height set to 100, border trimmed

Here is the rudimentatal code I have been using, I had something more elaborate, but I have gone back to basics to try and resolve this sizing issue:

<ControlTemplate TargetType="{x:Type TextBoxBase}">

    <ContentControl Margin="8">

        <Border Background="White"
                BorderBrush="red"
                BorderThickness="1"
                CornerRadius="2"
                Height="{TemplateBinding Height}"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch">

                <Label Content="Text Area..." />

        </Border>

    </ContentControl>

</ControlTemplate>

I didnt use a content control previously, this is just the latest container I have tried to get around this issue.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video