Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XAML how to say: default width and height for e.g. TextBox

So I'm coming at WPF from a HTML perspective.

I just want to put a TextBox on my Window like this:

<Grid>
    <TextBox Name="theName" />
</Grid>

Turns out that the TextBox is then HUGE, covers the whole window. (!)

Ok, that's not what I want, but I don't want to define the EXACT size either since I know Height and Width should be flexible, so I try:

<TextBox Name="theName" Width="Auto" Height="Auto"/>

Same thing. So I try:

<TextBox Name="theName" 
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Stretch"/>

Same thing. So I just hard code the sizes:

<TextBox Name="theName" Width="100" Height="20"/>

Which I know is not a good programming practice in WPF.

So, what how do you tell TextBox to "display default sizes for the font size being used"?

like image 427
Edward Tanguay Avatar asked Feb 02 '09 13:02

Edward Tanguay


People also ask

How do I make a textBox in XAML?

Here's how to create a TextBox in XAML and in code. TextBox textBox = new TextBox(); textBox. Width = 500; textBox. Header = "Notes"; textBox.

What is width and height in WPF?

The Height and the Width of window is 300 and 400 pixels. The type of Width and Height is a double device-independent unit (1/96th inch). This value can be followed by strings px, in, cm, or pt that a pixel, inch, centimeter, or point respectively. Here is a listing of pixels and other measurements. 1 inch = 96 pixels.

How do you draw a horizontal line in XAML?

Creating a Line The Line element in XAML creates a line shape. The following code snippet creates a Line by setting its start point (X1, Y1) to (50, 50) and end point (X2, Y2) to (200, 200). That means a line is drawn from point (50, 50) to (200, 200). The code also sets the color of the line to red and width 4.


2 Answers

You can take Bryan's example even a bit further. By specifying a specific alignment that isn't stretch and further constrain the TextBox so that it won't expand beyond a certain size. eg:

<Grid x:Name="LayoutRoot">
        <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Text="TextBox" TextWrapping="Wrap" 
        MinWidth="15" MinHeight="20" MaxWidth="500" MaxHeight="50"/>
</Grid>

You can take it even further by setting up rows/columns inside the Grid and constraining them in various fashions. As you're coming from an HTML background, think of it like using a table to control layout. Remember that you can also nest other container objects (i.e. StackPanels, WrapPanels, other Grids, etc...).

The challenge with XAML and the WPF/Silverlight controls is that they a very flexible, so you've got to get a handle on all the options and how they affect layout.

Good luck. I'm going through this exact same thing now.

like image 123
Steve Brouillard Avatar answered Sep 30 '22 08:09

Steve Brouillard


Use a different container. The Grid always streches its child controls to fill the grid cell.

You could use e.g. a stackpanel which only streches its controls in one direction.

like image 27
Stefan Avatar answered Sep 30 '22 09:09

Stefan