2

I am currently learning C# and XAML for UWP apps and have a problem with my TextBoxes.

How do I get the TextBox smaller? Adjusting the font size alone is not enough, even setting the height doesn't bring about any changes. What I noticed is that the Clear button doesn't get smaller, also the X in it doesn't get smaller with the FontSize. And that's probably the problem, because the TextBox probably doesn't want to cut the Clear button and therefore retains the default size, because you can make the box bigger without any problems, but not smaller.

I might add that I use Visual Studio Community 2017 and the TextBox from the existing ToolBox there.

Could someone help me with that? All I've found so far is how to add such a clear button and only on WPF apps.

7
  • 2
    You can copy the current TextBox's style from the generic.xaml and modify it to resize the TextBox and clear button so they are smaller Commented Nov 11, 2017 at 23:22
  • 4
    You can get the style here. Check that your issue isn't being caused by the TextBox's MinHeight property Commented Nov 11, 2017 at 23:25
  • Oh wow, that now answers some of the questions I asked myself, but it also called new ones. So you have to "copy" the complete design and then edit the properties you want to edit? So you're practically making your "own control element"? Is that the only way to handle this? Isn't there something like CSS where you just type in the children's elements and change the property like:"TextBox.Grid.ButtonGrid.DeleteButton.Height=20" or something like that? But of course I thank you for your help! Commented Nov 11, 2017 at 23:41
  • Unfortunately not, it can be a pain if you need to have a few differently styled UIElements. However if you set up your Styles correctly you can do the things you are saying using TemplateBinding's. Commented Nov 11, 2017 at 23:48
  • Well, good to know anyway. It's working now at least! Thanks for your help again! Commented Nov 11, 2017 at 23:54

1 Answer 1

2

As jsmyth886 said the MinHeight property will limit the minimum height you can set. If you want to make smaller TextBox, you could create custom style for TextBox like following

<Style TargetType="TextBox" x:Key="MyTextBoxStyle"> <Setter Property="MinHeight" Value="20"/> </Style> 

When you set the Height property to more than the MinHeight, it will take effect.

<TextBox x:Name="MyTextBox" Text="This text box" Style="{StaticResource MyTextBoxStyle}" Height="21" /> 

The above operation is also suitable to MinWidth property.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey! Yeah I see that there could be a problem with the limitation of the minheight property, but the funny thing is, that it doesnt affect to my uwp app. The only problem I had was the clear all button from the textbox which didn't react to my fontsize but thought it would ignore it, because it will fit. Therefore I had to adjust the height of the button myself, so that the TextBox could get smaller. However, I didn't know that you have to edit the whole template, or the copy of it at least, for such a fine adjustment.
The reason the button ignores the fontsize change is because it's got a hardcoded fontsize of 12 inside the TextBox's default style. You would need to edit that inner DeleteButton style so it uses a TemplateBinding fontsize
Hey! Yeah, I've already done that after you told me that I have to create a copy of the specific template which I can edit afterwards. I just wanted to say that the minheight property wasn't the problem, which one could also assume, but just the clear all button.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.