SharedSizeScope on a Grid makes the form 'dance' - wpf

I have a StackPanel (tried with a Grid as well) on my page.
<StackPanel Grid.IsSharedSizeScope="True">
<Partials:BaseInfo x:Name="baseInfo" />
<Partials:ExtraInfo x:Name="extraInfo" />
</StackPanel>
Both partials are using a grid with the same ColumnDefinitions:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90" SharedSizeGroup="C1" />
<ColumnDefinition Width="Auto" SharedSizeGroup="C2" />
<ColumnDefinition Width="Auto" SharedSizeGroup="C3" />
<ColumnDefinition Width="Auto" SharedSizeGroup="C4" />
<ColumnDefinition Width="Auto" SharedSizeGroup="C5" />
<ColumnDefinition Width="Auto" SharedSizeGroup="C6" />
<ColumnDefinition Width="Auto" SharedSizeGroup="C7" />
<ColumnDefinition Width="Auto" SharedSizeGroup="C8" />
</Grid.ColumnDefinitions>
Somehow 'C5' is acting up. If i set a fixes size there, everything works. If i leave it at auto, both the designer and the app will jump left/right in something that looks like an attempt to adjust the widths. The columns size up and down like crazy. I also tried with Width="*" and without a Width.
If i only Share the first 4 groups, everthing is fine as well. Also i use some ColumnSpans across different coulmns, which are different in the two usercontrols
What gives?

Related

How to make binded grid size to be proportionate value?

<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Width1}*" />
<ColumnDefinition Width="{Binding Width2}*" />
<ColumnDefinition Width="{Binding Width3}*" />
</Grid.ColumnDefinitions>
I basically want whatever the value of Width1, Width2 ... to have an asterisk after it.
So let's say the Width1 is 5, I want it to be 5* so that it will be a proportionate value.
I'm fairly new too WPF myself but I would do something like this:
And then you can do your binding on the WidthValString:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding WidthValString}" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
I'm assumming you will do this with ColumnDefinitions because you cannot have a grid with a width of 5*. As far as I know it has to be an integer. (With ColumnDef/RowDefs you'll be fine)
If this is not what you want, please specify your question!

UserControl cropped when used in MainWindow

Hello Stackoverflowers !
I'm creating a list with a searchbar in XAML, and when i tried to use my new UserControl in MainWindow.xaml, the Button in the UserControl is cropped in the designer instead of being resized to fit, why is that ?
Here is the MainWindow.xaml body :
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="8*" />
</Grid.ColumnDefinitions>
<controls:SearchBox Grid.Column="0"/>
</Grid>
And here is the body of the custom control :
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="8*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox
Grid.Row="0"
VerticalAlignment="Center"
Text="Search"
TextBlock.FontStyle="Italic"
TextBlock.TextAlignment="Left"
/>
<!-- Content="{StaticResource FilterLogo}" -->
<Button
Grid.Column="1"
Content="Filter"
VerticalAlignment="Center"
HorizontalAlignment="Stretch">
</Button>
<ListBox
Grid.Row="1"
Grid.ColumnSpan="2"
ItemsSource="{Binding DataContext}"
SelectedItem="{Binding DataContext}"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
I don't really know why my control is cropped to be honest. Any lead please ?
Short answer: it doesn't fit.
You have two columns in your main window. Since you have not specified which column to place the UserControl, it defaults to the first column. But your Width definitions force that column to be one fourth the size of the second column.
Your SearchBox simply doesn't fit in the space you have allotted to it.
If you remove the star (*) from this line:
<ColumnDefinition Width="8*" />
...it will fit without being cropped. (Alternatively, you could place the user control in the second column, or expand the main window, or shrink the width of your UserControl.)
When using stars in the column and row definitions, use them like percentages. Like this:
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".1*" /> <!-- 10% -->
<ColumnDefinition Width=".9*" /> <!-- 90% -->
</Grid.ColumnDefinitions>
This is what the XAML reader interpreted from your usage:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*" /> <!-- 800% -->
<ColumnDefinition Width="*" /> <!-- 100% -->
</Grid.ColumnDefinitions>
This will have undesired results.

Auto Sizing between grid columns

I have two controls and a GridSplitter .
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<UserControlOne Grid.Colum="0" Visibility="{Binding MyProperty1}"/>
<GridSplitter Visibility="{Binding MyProperty1}" m:Splitterbehaviour.Apply= true/>
<UserControlTwo Grid.Colum="1" />
</Grid>
I am trying to show/hide the UserControlOne with the MyProperty1 which is working fine but when it is hidden i want the UsercontrolTwo to take whole page space. I could easily achieve this by using a stack or dock panel. But if i use the stackpanel or dockpanel my GridSplitter wont work.(I have a behaviour set to GridSplitter which will identify the first column and it will help to resize the first and second column)
I don't see how that splitter is working
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<UserControlOne Grid.Colum="0" Visibility="{Binding MyProperty1}"/>
<UserControlTwo Grid.Colum="1"/>
</Grid>

XAML for Three way splitter in WPF

I want to split the main window in three way Grid Spliiter same alike outlook style.
and all the controls nested inside in the three window is resized using Grid Splitter.
I am trying to do that using defining Row Definition of Grid and placing each control in Row 1 or column 1..
Its nice to have , if any body has some kind of template to kick start with this kind of layout.. I don't want too complex sample..
This is a perfect resource - Build an Outlook 2007 UI Clone
You can download the full demo(OutlookUI HOL Basic - Final), I think it is what you are looking for
Edit(after comment)
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" Width="1" />
<GridSplitter Grid.Column="3" Width="1" />
</Grid>

What does the WPF star do (Width="100*")

What does exactly the star in size terms in WPF mean?
In a WPF Grid, Width="*" or Height="*" means proportional sizing.
For example: to give 30% to column 1 and 70% to column 2 -
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="7*" />
And likewise for rows -
<RowDefinition Height="3*" />
<RowDefinition Height="7*" />
The numbers do not have to be integers.
If the Width for RowDefinition (Height for ColumnDefinition) is omitted, 1* is implied.
In this example, column 1 is 1.5 times wider than column 2 -
<ColumnDefinition Width="1.5*" />
<ColumnDefinition />
You can mix auto-fit and fixed widths with * (proportional) widths; in that case the * columns are apportioned to the remainder after the auto-fit and fixed widths have been calculated -
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" /> <!-- Auto-fit to content, 'Hi' -->
<ColumnDefinition Width="50.5" /> <!-- Fixed width: 50.5 device units) -->
<ColumnDefinition Width="69*" /> <!-- Take 69% of remainder -->
<ColumnDefinition Width="31*"/> <!-- Take 31% of remainder -->
</Grid.ColumnDefinitions>
<TextBlock Text="Hi" Grid.Column="0" />
If you have 2 columns like this:
<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="*"/>
it means that the first column is 10x wider than the second. It's like saying "10 parts column 1, and 1 part column 2."
The cool thing about this is that your columns will resize proportionally. Other options are:
//Take up as much space as the contents of the column need
<ColumnDefinition Width="Auto"/>
//Fixed width: 100 pixels
<ColumnDefinition Width="100"/>
Hope that helps!
we take following example.....
A grid has 3 columns each containing one button of width 100.
XAML Code is...
<Grid x:Name="LayoutRoot" Width="600">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="100" />
<Button Content="Button1" Height="23" HorizontalAlignment="Left" Margin="0,10,0,0" Name="button2" VerticalAlignment="Top" Width="100" Grid.Column="1" />
<Button Content="Button2" Height="23" HorizontalAlignment="Left" Margin="0,10,0,0" Name="button3" VerticalAlignment="Top" Width="100" Grid.Column="2" />
</Grid>
But actually its size is....
<Grid.ColumnDefinitions>
<ColumnDefinition Width="375" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="125" />
</Grid.ColumnDefinitions>
Conclusions:
Total size of grid is 600
Auto : Column is resizes based on it's contents. (2nd column has button of width 100)
* : 1st column width is 3x of 3rd column.
In addition, you can leave out the "*" if that's the element of unit size. So using Pwninstein's code example, it would just be:
<ColumnDefinition Width="10*/>
<ColumnDefinition/>

Resources