Introduction
Aligning controls on UI is a skill and a bit of a headache too.
One might go ahead and add a margin to every single control on-screen so that he/she can align them in a single line.
Here, they have to ensure that controls are aligned pixel by pixel. Being a developer, we might miss pixel or two & we won't even notice it.
IsSharedSizeScope is used to keep the control's alignment consistent between Panel's children.
Imagine a screen with hard-coded margins for every TextBox. It is always a bad practice, because as soon as the width of the main page or any content within page changes then all these hardcoded margins will stay unaffected of that change because of their hardcoded values.
Let me show you an example:
- <StackPanel x:Name="StackPanelOuter"
- Orientation="Vertical">
- <StackPanel x:Name="StackPanelInnerUserName"
- Orientation="Horizontal" >
- <Label x:Name="LabelUserName"
- Content="User Name:"
- Margin="0 10 0 0"/>
- <TextBox x:Name="TextBoxUserName"
- Text="{Binding UserName}"
- Height="20"
- Width="150"
- Margin="50 10 0 0"/>
- </StackPanel>
- <StackPanel x:Name="StackPanelInnerPassword"
- Orientation="Horizontal">
- <Label x:Name="LabelPassword"
- Content="Password:" />
- <PasswordBox x:Name="TextBoxPassword"
- Height="20"
- Width="150"
- Margin="60 0 0 0" />
- </StackPanel>
- <StackPanel x:Name="StackPanelInnerConfirmPassword"
- Orientation="Horizontal">
- <Label x:Name="LabelConfirmPassword"
- Content="Confirm Password:" />
- <PasswordBox x:Name="TextBoxConfirmPassword"
- Height="20"
- Width="150"
- Margin="14 0 0 0" />
- </StackPanel>
- <StackPanel x:Name="StackPanelInnerEmail"
- Orientation="Horizontal">
- <Label x:Name="LabelEmailId"
- Content="Email:" />
- <TextBox x:Name="TextBoxEmail"
- Height="20"
- Width="150"
- Margin="80 0 0 0"/>
- </StackPanel>
- <Button x:Name="ButtonLogin"
- Height="20"
- Width="100"
- Content="Register"
- HorizontalAlignment="Center"
- Margin="20 10 0 0"
- Command="{Binding RegisterButtonClicked}"
- />
- <TextBlock x:Name="TextBlockMessage"
- Visibility="{Binding IsButtonClicked, Converter={StaticResource BooleanToVisibilityConverter}}"
- Text="{Binding UserName, StringFormat='User: {0} is successfully registered!'}"
- HorizontalAlignment="Center"
- Margin="20 8 0 0"
- />
- </StackPanel>
When you run the program, you will get an output as follows:

Everything works smoothly, there are no required changes.
Now let's say that tomorrow our client wants to change 3 labels.
- Confirm Password to Password
- User Name to Name
- Email to Email Address
If we do that, our screen would look like this:

Now the developer again has to change Margins of these TextBoxes, and if the client asks for more changes then again developer has to change margins as per new requirements.
We can't really blame client here, it is in their blood to change requirements constantly, So we have to be flexible enough to write code which is extensible with the new requirements.
So to overcome this problem of hard-coded margin we can use SharedSizeScope in WPF.
We should let XAML handle this situation, And we can simply use IsSharedSizeScope Attached property of Grid panel.


Tanya GeorgievaPosted Jul 8, 2020, 2:52 AM
Hello, thank you for your article. I would have a question about that property (SharedSizeGroup) - is it true that the use of SharedSizeGroup could increase the CPU usage ? Thank you.