Moses Aprico
Moses Aprico

Reputation: 2121

How to make controls fill the container in WPF?

Below is my xaml code :

<Window x:Class="ScoresBank.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<DockPanel LastChildFill="True">
    <WrapPanel DockPanel.Dock="Top">
        <ToolBar Height="25"></ToolBar>
    </WrapPanel>
    <WrapPanel DockPanel.Dock="Bottom">
        <StatusBar Name="stbBottom" Height="25"
                   BorderThickness="2"
                   BorderBrush="Black">Example</StatusBar>
    </WrapPanel>
    <StackPanel DockPanel.Dock="Left">
        <DockPanel LastChildFill="True">
            <WrapPanel DockPanel.Dock="Top">
                <Button Name="btnBrowseTags">TAGS</Button>
                <Button Name="btnBrowseTitles">TITLES</Button>
            </WrapPanel>
            <ComboBox DockPanel.Dock="Top" Name="cbxCategory">
                Example
            </ComboBox>
            <WrapPanel DockPanel.Dock="Bottom">
                <TextBox Name="txtKeyword"></TextBox>
                <Button Name="btnFind">Find</Button>
            </WrapPanel>
            <ListBox Name="lbxBrowse">
            </ListBox>
        </DockPanel>
    </StackPanel>
    <StackPanel DockPanel.Dock="Right">
        <Button>Example</Button>
    </StackPanel>
    <Canvas>

    </Canvas>
</DockPanel>

And this is my current layout view : enter image description here

So, what I mean with filling the container are :

  1. Making lbxBrowse to fill the mid-space of the DockPanel inside the left StackPanel.
  2. Making txtKeyword to fill the WrapPanel.
  3. Making stbBottom to fill the WrapPanel.

What I've tried :

  1. It seems i've put them in a DockPanel with LastChildFill="True". But it seems doesn't work.
  2. I don't know about this, since it's not possible to put it into a DockPanel first.
  3. I don't know anything about this.

I don't use fixed size, since I need them to keep neat even when resized in multiple screen resolution. The fixed size on ToolBar and StatusBar seems required, otherwise, the text will be unseen.

P.S. If possible, I prefer the solution to be XAML code, rather than the C# code. Otherwise, C# code is fine too.

Thank you.

Upvotes: 0

Views: 2559

Answers (2)

Marius Bancila
Marius Bancila

Reputation: 16328

You should use a Grid. It is more easier to configure. Here is an example (I don't know exactly how you want to setup your layout).

<Window x:Class="SampleWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="400" Width="600">
    <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="Auto" />
         <RowDefinition Height="*" />
         <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
         <ColumnDefinition Width="Auto" />
         <ColumnDefinition Width="Auto" />
         <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>

      <Button Margin="5" Content="TAGS"
               Grid.Column="0" Grid.Row="0" />
      <Button Margin="5" Content="TITLES"
               Grid.Column="1" Grid.Row="0" />
      <Button Margin="5" Content="EXAMPLES"
              Grid.Column="2" Grid.Row="0" 
              HorizontalAlignment="Right"/>

      <ComboBox Margin="5"
                 Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" />

      <ListBox Margin="5"
                Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" />

      <Button Margin="5" Content="EXAMPLE"
              Grid.Column="2" Grid.Row="3" HorizontalAlignment="Left" />

   </Grid>
</Window>

And the result:

enter image description here

With a grid you can set the height and the width of the columns and rows to a specific value (in points, pixels, cm, etc.), to column content (Auto) or proportional to the grid (with *).

Upvotes: 2

Koscik
Koscik

Reputation: 189

Instead of using a StackPanel and DockPanel you can use Grid and set Grid.ColumnDefinitions and Grid.RowDefinitions. You can specify directly each row Height and each column Width. It's easier to use and it automaticly fit to content and container.

Upvotes: 2

Related Questions