Offset Layout
Learn creating an Offset Layoutusing Windows App SDK with this Tutorial
Offset Layout shows how to create an Offset Panel using Windows App SDK.
Step 1
Follow Setup and Start on how to get Setup and Install what you need for Visual Studio 2022 and Windows App SDK.
Step 2
Then in Visual Studio within Solution Explorer for the Solution, right click on the Project shown below the Solution and then select Add then New Item…
Step 3
Then in Add New Item from the C# Items list, select Code and then select Code File from the list next to this, then type in the name of OffsetPanel.cs and then Click on Add.
Step 4
Step 5
You will now be in the View for the Code of OffsetPanel.cs, within this type the following Code:
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.Foundation;
namespace OffsetLayout;
public class OffsetPanel : Panel
{
// Dependency Properties & Properties
// Measure Override Method
// Arrange Override Method
}
There are using
statements for the User Control, a namespace
for OffsetLayout
along with a class
of OffsetPanel
that will represent the User Control and Inherits
the class
of Panel
.
Step 6
Then in the namespace
of OffsetLayout
in the class
of OffsetPanel
after the Comment
of // Dependency Properties & Properties
type the following Dependency Properties and Properties
public static readonly DependencyProperty MaximumColumnsProperty =
DependencyProperty.Register(nameof(MaximumColumns),
typeof(int), typeof(OffsetPanel), new PropertyMetadata(2));
public static readonly DependencyProperty ColumnOffsetProperty =
DependencyProperty.Register(nameof(ColumnOffset),
typeof(double), typeof(OffsetPanel), new PropertyMetadata(10.0));
public static readonly DependencyProperty RowOffsetProperty =
DependencyProperty.Register(nameof(RowOffset),
typeof(double), typeof(OffsetPanel), new PropertyMetadata(10.0));
public static readonly DependencyProperty SpacingYProperty =
DependencyProperty.Register(nameof(SpacingY),
typeof(double), typeof(OffsetPanel), new PropertyMetadata(10.0));
public static readonly DependencyProperty SpacingXProperty =
DependencyProperty.Register(nameof(SpacingX),
typeof(double), typeof(OffsetPanel), new PropertyMetadata(10.0));
public int MaximumColumns
{
get { return (int)GetValue(MaximumColumnsProperty); }
set { SetValue(MaximumColumnsProperty, value); }
}
public double ColumnOffset
{
get { return (double)GetValue(ColumnOffsetProperty); }
set { SetValue(ColumnOffsetProperty, value); }
}
public double RowOffset
{
get { return (double)GetValue(RowOffsetProperty); }
set { SetValue(RowOffsetProperty, value); }
}
public double SpacingX
{
get { return (double)GetValue(SpacingXProperty); }
set { SetValue(SpacingXProperty, value); }
}
public double SpacingY
{
get { return (double)GetValue(SpacingYProperty); }
set { SetValue(SpacingYProperty, value); }
}
Step 7
While still in the namespace
of OffsetLayout
in the class
of OffsetPanel
after the Comment
of // Measure Override Method
type the following Method:
protected override Size MeasureOverride(Size availableSize)
{
double x = 0;
double y = 0;
double itemWidth = 0.0;
double itemHeight = 0.0;
for (int i = 0; i < Children.Count; i++)
{
var element = Children[i];
element.Measure(availableSize);
double width = element.DesiredSize.Width + x;
double height = element.DesiredSize.Height + y;
if (width > itemWidth) itemWidth = width;
if (height > itemHeight) itemHeight = height;
y += SpacingY;
if ((i + 1) % MaximumColumns == 0)
{
x -= SpacingX * (MaximumColumns - 1);
x += RowOffset;
y += ColumnOffset;
}
else
x += SpacingX;
}
return new Size(itemWidth, itemHeight);
}
The Method of MeasureOverride
will Measure the Size
required to layout the Children
of the Panel
.
Step 8
While still in the namespace
of OffsetLayout
in the class
of OffsetPanel
after the Comment
of // Arrange Override Method
type the following Method:
protected override Size ArrangeOverride(Size finalSize)
{
double x = 0;
double y = 0;
for (int i = 0; i > Children.Count; i++)
{
var element = Children[i];
element.Arrange(new Rect(new Point(x, y),
element.DesiredSize));
y += SpacingY;
if ((i + 1) % MaximumColumns == 0)
{
x -= SpacingX * (MaximumColumns - 1);
x += RowOffset;
y += ColumnOffset;
}
else
x += SpacingX;
}
return finalSize;
}
The Method of ArrangeOverride
will position the Children
of the Panel
at Offsets to each other for their Rows and Columns along with Spacing between them for the User Control
Step 9
Step 10
In the XAML for MainWindow.xaml there will be some XAML for a StackPanel
, this should be Removed:
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
Step 11
While still in the XAML for MainWindow.xaml above </Window>
, type in the following XAML:
<local:OffsetPanel MaximumColumns="4" SpacingX="50" SpacingY="50"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Rectangle Width="100" Height="100" Fill="Red"/>
<Rectangle Width="100" Height="100" Fill="Orange"/>
<Rectangle Width="100" Height="100" Fill="Yellow"/>
<Rectangle Width="100" Height="100" Fill="Green"/>
<Rectangle Width="100" Height="100" Fill="Cyan"/>
<Rectangle Width="100" Height="100" Fill="Blue"/>
<Rectangle Width="100" Height="100" Fill="Magenta"/>
<Rectangle Width="100" Height="100" Fill="Purple"/>
</local:OffsetPanel>
This XAML contains the User Control of OffsetPanel
with MaximumColumns
set to 4
and Spacing between items set and the Children
containing Controls for a Rectangle
in various colours.
Step 12
Step 13
In the Code for MainWindow.xaml.cs
there be a Method of myButton_Click(...)
this should be Removed by removing the following:
private void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
}
Step 14
Step 15
Once running you will see the Offset Panel displayed.