WPF - Layouts design - wpf

I'm trying to design a layout in a WPF window but I'm having a bit of trouble. enter image description here
That is the layout Im after. Ive tried grids, stack panels, etc.. but I cannot get the right docking or they overlap.
Any ideas ?? Thanks!

First make 2 rows , then 2nd row with 3 cols
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20*"/>
<RowDefinition Height="80*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="60*"/>
<ColumnDefinition Width="20*"/>
</Grid.ColumnDefinitions>
</Grid>
</Grid>

Related

Textbox alignment inside Groupbox (WPF)

I'm having trouble with placing a textbox inside a groupbox (which is inside a grid). Since groupbox can contain only one children I created a grid to place labels and textboxes. The problem is - the textboxes created inside the groupboxes are left aligned according to the 2nd column of the grid inside the groupbox. I need the textboxes to be aligned left in the 2nd column of the main grid.
My xaml code
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
<GroupBox.Header>
<Label Content="Header"></Label>
</GroupBox.Header>
<Grid Grid.Row="0" Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Label1:"/>
<TextBox Grid.Column="1" />
<Label Grid.Column="0" Grid.Row="1" Content="Label2:"/>
<TextBox Grid.Row="1" Grid.Column="1"/>
</Grid>
</GroupBox>
</Grid>
Output for this code
You could see the textboxes in the column 1 of the main grid. If you look at the image, there is a radio button below that row which is in the second column. I need the textboxes to be aligned with that radiobutton I need them in the second column.
Width="Auto" means the Column will automatically take a width.
You need to use something like this instead;
`<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>`
Have a look at this ColumnWidths for further explanation if needed.
To move the TextBoxes to the next Column in the main Grid, you need to remove them from the GroupBox and add them to the main Grid instead. You haven't posted the whole XAML so it's difficult to advise further.
if there are going to be only two columns in your grid then just below column definitions for your inner Grid:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>
As you want to align with the radio buttons that must be in second column of main grid then for ColSpan=2 this will perfectly.
When Width is set to Auto for a ColumnDefinition of a Grid it
will take the width of element defined in the Column , So that's
the first thing you should remove a you are not using fixed Width
for TextBoxes.

Fill Middle Grid Column with Slider

I have a 3 column Grid -
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
I am trying to fill the * column with a slider control - I have tried DockPanel and StackPanel - cannot get the slider width to fill that middle column. The slider is for a media element position changer that loads an .avi to a set height in xaml - so the aspect ratio is taken care of. But...Width changes to the correct ratio...the Grid size changes so I cant set a Width in xaml for the slider control.
I am not sure if that is exactly what you need, because it looks so obvious to me !
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Slider Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
that gonna fill the second column :

Define Column Width as a rest of unused pixels

I would like to define two columns. Second one 1280px wide and the first one the rest of unused pixels. How to do it?
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="9*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="REST OF THE WIDHT" />
<ColumnDefinition Width="1280" />
</Grid.ColumnDefinitions>
</Grid>
You were on to the right idea with your RowDefinition
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="9*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1280" />
</Grid.ColumnDefinitions>
</Grid>
Star sizing basically tells the layout engine how you would like to divide up the space available to the parent element amount the children, in terms of proportions.
Here are a couple articles that may help:
Star Sizing
WPF Tutorial
Are you sure you want hard coded widths? Resize your browser while running it and see how it looks. If you use "Auto" for one column or row and then "1*" for the other, it will fill the "Auto" with what is needed and then the rest goes to the second one.
I would think this might work better for you:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
</Grid>
Then you start to make use of ScrollViewers for the content that might be bigger than its container.

WPF AutoCompleteBox Width based on Content

I have an System.Windows.Controls.AutoCompleteBox with a fixed width.
The width should dynamically grow based on the content.
How can I achieve this.
Thanks for your help
You can achieve it by placing the AutoCompleteBox in a Grid.Column with Width="Auto". Here's an example:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="30" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<toolkit:AutoCompleteBox />
</Grid>

Silverlight Grid Layout

When I have Grid in Silverlight, and I provide Column Definitions like below
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
For some reason the items that get placed in those columns get cut off.
That is I only see half the control.
But when I do
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
and put items in those respected rows I can see the entire items with their proper respective widths and heights.
What could I be overlooking?
Thanks
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
is actually just a short cut for
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
which means, you have two columns in this Grid, each takes 50% of the width.
Same way,
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
is the same as
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
Hope this helps. :)
Look for following link. It should help you:
Using the Grid control in Silverlight

Resources