Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a WPF window into two parts?

Tags:

c#

wpf

xaml

I want to create an app that has a listbox on the left side (I'll style it to make it look good later).

On the right side, I want to have an area where I can add controls, etc

So the question is what do I need to do to split the Window into two unequal parts (left side approx 350 pixels wide and height should be the entire window) and the remainder is for my "canvas."

like image 469
Cocoa Dev Avatar asked Jun 09 '11 15:06

Cocoa Dev


2 Answers

You can use a Grid:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="350" /> <!-- Or Auto -->
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <ListBox Grid.Column="0" />
    <Canvas Grid.Column="1" />
</Grid>

Or you could use a DockPanel:

<DockPanel>
    <ListBox DockPanel.Dock="Left" Width="350" />
    <Canvas />
</DockPanel>

The benefit of the Grid is you have finer control over the layout and could allow the end user to dynamically resize the columns using a GridSplitter.

like image 105
CodeNaked Avatar answered Nov 15 '22 02:11

CodeNaked


An alternate approach to CodeNaked's solution can be to use DockPanel where Canvas takes all space that is left automatically and you don't have to work out splits.

Of course this has the limitation of docking only to the four edges (with possibility of stacking up at edges) but I tend to prefer DockPanel when I'm making initial UI as they're rather quick and easy to setup compared to Grid setup which can get complex fairly quickly.

<DockPanel LastChildFill="True">
    <ListBox DockPanel.Dock="Left" Width="350"/>
    <Canvas />
</DockPanel>
like image 32
Maverik Avatar answered Nov 15 '22 04:11

Maverik