Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a control fill all available space

Tags:

c#

wpf

xaml

I have a xaml code:

<Grid>
    <WrapPanel>
    <TextBox ></TextBox>
    <Button Content="GetIt" />
    </WrapPanel>
</Grid>

How i can to get all available space for textBox?

i want to do something like that:

|[____________________][GetIt]|

like image 466
Neir0 Avatar asked Mar 13 '10 17:03

Neir0


People also ask

How many UI containers are available with WPF?

User Interface Panels. There are six panel classes available in UI scenarios: Canvas, DockPanel, Grid, StackPanel, VirtualizingStackPanel, and WrapPanel.

What is a StackPanel WPF?

A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.

What is WPF viewbox?

Defines a content decorator that can stretch and scale a single child to fill the available space.


3 Answers

There are a number of ways this can be achieved, including this one:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <TextBox />
  <Button Grid.Column="1">GetIt</Button>
</Grid>
like image 85
Steve Greatrex Avatar answered Sep 27 '22 21:09

Steve Greatrex


Try this:

<Grid>
    <TextBox HorizontalAlignment="Stretch" Margin="2,2,102,2"></TextBox>
    <Button HorizontalAlignment="Right" Width="100" Content="GetIt" />
</Grid>

Just make the button the desired width and the text box will fill up the rest.


Thanks for the catch; corrected above to properly handle margin on right. This does, however, require you to update the margin when the button width changes. Two columns is a better solution if you plan to change the spacing often. Using the margin is cleaner if you have several controls in the grid and don't want to create nested grids to handle this kind of split.

like image 21
Dan Bryant Avatar answered Sep 27 '22 20:09

Dan Bryant


The simplest way is to use a DockPanel instead of a Grid (the default for LastChildFill is true but I also added it here for clarity):

<DockPanel LastChildFill="True">
  <Button Content="GetIt" DockPanel.Dock="Right" />
  <TextBox ></TextBox>
</DockPanel>
like image 33
John Bowen Avatar answered Sep 27 '22 21:09

John Bowen