Uniform Control demonstrates how to create a UniformPanel where all the elements in the Panel are sized the same way based from the first item in the panel.
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in the Name as UniformControl and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as UniformPanel.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for UniformPanel.cs the following should be entered:
using System; using Windows.Foundation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace UniformControl { public class UniformPanel : Panel { private int _columns; private int _rows; public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(int), typeof(UniformPanel), new PropertyMetadata(0)); public static readonly DependencyProperty FirstColumnProperty = DependencyProperty.Register("FirstColumn", typeof(int), typeof(UniformPanel), new PropertyMetadata(0)); public static readonly DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(int), typeof(UniformPanel), new PropertyMetadata(0)); public int Columns { get { return (int)GetValue(ColumnsProperty); } set { SetValue(ColumnsProperty, value); } } public int FirstColumn { get { return (int)GetValue(FirstColumnProperty); } set { SetValue(FirstColumnProperty, value); } } public int Rows { get { return (int)GetValue(RowsProperty); } set { SetValue(RowsProperty, value); } } private void UpdateComputedValues() { _columns = Columns; _rows = Rows; if (FirstColumn >= _columns) FirstColumn = 0; if ((_rows == 0) || (_columns == 0)) { var row = 0; var column = 0; int count = Children.Count; while (column < count) { UIElement element = Children[column]; if (element.Visibility != Visibility.Collapsed) { row++; } column++; } if (row == 0) row = 1; if (_rows == 0) { if (_columns > 0) { _rows = ((row + FirstColumn) + (_columns - 1)) / _columns; } else { _rows = (int)Math.Sqrt(row); if ((_rows * _rows) < row) { _rows++; } _columns = _rows; } } else if (_columns == 0) { _columns = (row + (_rows - 1)) / _rows; } } } protected override Size ArrangeOverride(Size size) { Rect rectangle = new Rect(0.0, 0.0, size.Width / _columns, size.Height / _rows); double width = rectangle.Width; double value = size.Width - 1.0; rectangle.X += rectangle.Width * FirstColumn; foreach (UIElement element in Children) { element.Arrange(rectangle); if (element.Visibility != Visibility.Collapsed) { rectangle.X += width; if (rectangle.X >= value) { rectangle.Y += rectangle.Height; rectangle.X = 0.0; } } } return size; } protected override Size MeasureOverride(Size size) { UpdateComputedValues(); Size available = new Size(size.Width / (_columns), size.Height / (_rows)); double width = 0.0; double height = 0.0; int value = 0; int count = Children.Count; while (value < count) { UIElement element = Children[value]; element.Measure(available); Size desired = element.DesiredSize; if (width < desired.Width) width = desired.Width; if (height < desired.Height) height = desired.Height; value++; } return new Size(width * _columns, height * _rows); } } }
The UniformPanel Class Inherits the Panel class and then has Columns, FirstColumn and Rows properties. The UpdateComputedValues method gets the Columns and Rows and adjusts the layout accordingly based on the Visibility of the elements to produce the correct number of Rows and Columns needed. ArrangeOverride respnds to Size changes that require the layout to be updated and arranges these based from a Rect from the Width and Height plus the number of rows and columns. MeasureOverride returns a Size by first using UpdateComputedValues then setting the Available size and then resizing each Child of the control to be the desired Size value.
Step 8
Once done select from the Menu, Build, then Build Solution
Step 9
In the Solution Explorer select MainPage.xaml
Step 10
From the Menu choose View and then Designer
Step 11
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Viewbox Margin="50"> <local:UniformPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Rectangle Width="75" Height="75" Fill="Black" Margin="5"/> <Rectangle Width="75" Height="75" Fill="Red"/> <Rectangle Width="75" Height="75" Fill="Orange"/> <Rectangle Width="75" Height="75" Fill="Yellow"/> <Rectangle Width="75" Height="75" Fill="Green"/> <Rectangle Width="75" Height="75" Fill="Cyan"/> <Rectangle Width="75" Height="75" Fill="Blue"/> <Rectangle Width="75" Height="75" Fill="Magenta"/> <Rectangle Width="75" Height="75" Fill="Purple"/> </local:UniformPanel> </Viewbox>
The MainPage has a Viewbox with the UniformPanel Control itself with the Children set to a set of Rectangle Controls.
Step 12
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 13
After the Application has started it should then appear displaying the UniformPanel Control with some items where the size of each element is determined by the first item in the control
Step 14
To Exit the Application select the Close button in the top right of the Application
Uniform Control is based off the Uniform Grid in the WinRTXamlToolkit by Filip Skakun and is quite a useful control to have, in the example all the other items in the control are sized based from the initial element so they all have a uniform size – this makes it easy to create proportional layouts with the correct spacing based on the largest elements, something that might come in useful when creating applications.