Cocoa Dev
Cocoa Dev

Reputation: 9541

How do I split a WPF window into two parts?

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."

Upvotes: 7

Views: 24538

Answers (2)

Maverik
Maverik

Reputation: 5671

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>

Upvotes: 4

CodeNaked
CodeNaked

Reputation: 41393

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.

Upvotes: 12

Related Questions