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 pos开发者_如何学运维sible, and if so-- how can I go about this?
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
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.
精彩评论