2

I'd like my textbox to have a width that will fit exactly the size of the text within it.

So if there's just one character within it it'll be a small width. And if there's a long word within it it'll be a long width, etc.

Is this possible, and if so-- how can I go about this?

3 Answers 3

3

That is the default behavior of a TextBox, however the parent panel for the TextBox can affect a TextBox's size.

For example, a Grid will automatically stretch it's children to fill all available space. A DockPanel will do the same with the Last Child added to it, unless LastChildFill=False.

You can overwrite this behavior in the parent panel by setting the HorizontalAlignment of the TextBox. For example, setting HorizontalAlignment="Center" will center the TextBox inside a Grid rather than stretching it to fill all remaining space

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

Comments

0

It depends on the container the TextBox is in, but typically if you set HorizontalAlignment="Left" you will get this behavior.

However, if it's (for example) the last element in the DockPanel with the LastChildFill property set to true, then the DockPanel will stretch it out for you.

Comments

0

Go to the XAML code and set the width property to Auto. If it will just be one line this will work, but if you want two or more to be resized, set height also to Auto. For example:

\<...Width = "72" Height = "80".../\>

Set it to:

\<...Width = "Auto" Height = "Auto".../\>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.