Stack Panel Example for WPF

Nevron NOV UserInterface for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF UserInterface examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Stack Panel Example for WPF Description

This example demonstrates how to create a stack layout panel and add widgets to it. You can control the parameters of the layout algorithm using the controls to the right.


Stack Panel Example

C# Stack Panel Example Code for Nevron UserInterface for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Dom;
using Nevron.Nov.Editors;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Presentation;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NStackPanelExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			m_StackPanel = new NStackPanel();			
			m_StackPanel.SetBorder(1, NColor.Red);

            for (int i = 1; i <= 16; i++)
			{
				m_StackPanel.Add(new NButton("Button " + i.ToString()));
			}

			return m_StackPanel;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			
			// properties stack
			NList editors = NDesigner.GetDesigner(m_StackPanel).CreateMemberEditors(
				m_StackPanel,
				NStackPanel.EnabledProperty,
				NStackPanel.HorizontalPlacementProperty,
				NStackPanel.VerticalPlacementProperty,
				NStackPanel.DirectionProperty,
				NStackPanel.VerticalSpacingProperty,
				NStackPanel.HorizontalSpacingProperty,
				NStackPanel.FitModeProperty,
				NStackPanel.FillModeProperty,
				NStackPanel.UniformWidthsProperty,
				NStackPanel.UniformHeightsProperty
			);

			NStackPanel propertiesStack = new NStackPanel();
			for (int i = 0; i < editors.Count; i++)
			{
				NMemberEditor editor = (NMemberEditor)editors[i];
				if (editor.EditedMember == NStackPanel.HorizontalSpacingProperty)
				{
					editor.SetEditedMemberValue(NDesign.HorizontalSpacing);
				}
				else if (editor.EditedMember == NStackPanel.VerticalSpacingProperty)
				{
					editor.SetEditedMemberValue(NDesign.VerticalSpacing);
				}

				propertiesStack.Add(editor);
			}

			stack.Add(new NGroupBox("Properties", new NUniSizeBoxGroup(propertiesStack)));

			// items stack
			NStackPanel itemsStack = new NStackPanel();			
			NButton addSmallItemButton = new NButton("Add Small Item");
			addSmallItemButton.Click += OnAddSmallItemButtonClick;
			itemsStack.Add(addSmallItemButton);

			NButton addLargeItemButton = new NButton("Add Large Item");
			addLargeItemButton.Click += OnAddLargeItemButtonClick;
			itemsStack.Add(addLargeItemButton);

			NButton addRandomItemButton = new NButton("Add Random Item");
			addRandomItemButton.Click += OnAddRandomItemButtonClick;
			itemsStack.Add(addRandomItemButton);

			NButton removeAllItemsButton = new NButton("Remove All Items");
			removeAllItemsButton.Click += OnRemoveAllItemsButtonClick;
			itemsStack.Add(removeAllItemsButton);

			stack.Add(new NGroupBox("Items", itemsStack));

			return stack;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to create a stack layout panel and add widgets to it. You can control the parameters of the layout algorithm using the controls to the right.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Stack Panel", "https://www.nevron.com/docs/openvision/ui-stack-panel.html"), ("Panels Overview", "https://www.nevron.com/docs/openvision/ui-panels-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnAddSmallItemButtonClick(NEventArgs args) { NButton item = new NButton("Small." + m_StackPanel.Count.ToString()); item.MinSize = new NSize(5, 5); item.PreferredSize = new NSize(25, 25); m_StackPanel.Add(item); } private void OnAddLargeItemButtonClick(NEventArgs args) { NButton item = new NButton("Large." + m_StackPanel.Count.ToString()); item.MinSize = new NSize(20, 20); item.PreferredSize = new NSize(60, 60); m_StackPanel.Add(item); } private void OnAddRandomItemButtonClick(NEventArgs args) { int range = 30; Random rnd = new Random(); NButton item = new NButton("Random." + m_StackPanel.Count.ToString()); item.MinSize = new NSize(rnd.Next(range), rnd.Next(range)); item.PreferredSize = new NSize(rnd.Next(range) + range, rnd.Next(range) + range); m_StackPanel.Add(item); } private void OnRemoveAllItemsButtonClick(NEventArgs args) { m_StackPanel.Clear(); } #endregion #region Fields private NStackPanel m_StackPanel; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Stack Panel Example for WPF Solution

VB.NET Stack Panel Example Code for Nevron UserInterface for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Editors
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NStackPanelExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            m_StackPanel = New NStackPanel()
            m_StackPanel.SetBorder(1, NColor.Red)

            For i = 1 To 16
                m_StackPanel.Add(New NButton("Button " & i.ToString()))
            Next

            Return m_StackPanel
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()

            ' properties stack
            Dim editors = NDesigner.GetDesigner(m_StackPanel).CreateMemberEditors(m_StackPanel, NInputElement.EnabledProperty, NWidget.HorizontalPlacementProperty, NWidget.VerticalPlacementProperty, NStackPanel.DirectionProperty, NStackPanel.VerticalSpacingProperty, NStackPanel.HorizontalSpacingProperty, NStackPanel.FitModeProperty, NStackPanel.FillModeProperty, NStackPanel.UniformWidthsProperty, NStackPanel.UniformHeightsProperty)

            Dim propertiesStack As NStackPanel = New NStackPanel()
            For i = 0 To editors.Count - 1
                Dim editor = editors(i)
                If editor.EditedMember Is NStackPanel.HorizontalSpacingProperty Then
                    editor.SetEditedMemberValue(NDesign.HorizontalSpacing)
                ElseIf editor.EditedMember Is NStackPanel.VerticalSpacingProperty Then
                    editor.SetEditedMemberValue(NDesign.VerticalSpacing)
                End If

                propertiesStack.Add(editor)
            Next

            stack.Add(New NGroupBox("Properties", New NUniSizeBoxGroup(propertiesStack)))

            ' items stack
            Dim itemsStack As NStackPanel = New NStackPanel()
            Dim addSmallItemButton As NButton = New NButton("Add Small Item")
            addSmallItemButton.Click += AddressOf OnAddSmallItemButtonClick
            itemsStack.Add(addSmallItemButton)

            Dim addLargeItemButton As NButton = New NButton("Add Large Item")
            addLargeItemButton.Click += AddressOf OnAddLargeItemButtonClick
            itemsStack.Add(addLargeItemButton)

            Dim addRandomItemButton As NButton = New NButton("Add Random Item")
            addRandomItemButton.Click += AddressOf OnAddRandomItemButtonClick
            itemsStack.Add(addRandomItemButton)

            Dim removeAllItemsButton As NButton = New NButton("Remove All Items")
            removeAllItemsButton.Click += AddressOf OnRemoveAllItemsButtonClick
            itemsStack.Add(removeAllItemsButton)

            stack.Add(New NGroupBox("Items", itemsStack))

            Return stack
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to create a stack layout panel and add widgets to it. You can control the parameters of the layout algorithm using the controls to the right.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Stack Panel", "https://www.nevron.com/docs/openvision/ui-stack-panel.html"), ("Panels Overview", "https://www.nevron.com/docs/openvision/ui-panels-overview.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnAddSmallItemButtonClick(args As NEventArgs) Dim item As NButton = New NButton("Small." & m_StackPanel.Count.ToString()) item.MinSize = New NSize(5, 5) item.PreferredSize = New NSize(25, 25) m_StackPanel.Add(item) End Sub Private Sub OnAddLargeItemButtonClick(args As NEventArgs) Dim item As NButton = New NButton("Large." & m_StackPanel.Count.ToString()) item.MinSize = New NSize(20, 20) item.PreferredSize = New NSize(60, 60) m_StackPanel.Add(item) End Sub Private Sub OnAddRandomItemButtonClick(args As NEventArgs) Dim range = 30 Dim rnd As Random = New Random() Dim item As NButton = New NButton("Random." & m_StackPanel.Count.ToString()) item.MinSize = New NSize(rnd.Next(range), rnd.Next(range)) item.PreferredSize = New NSize(rnd.Next(range) + range, rnd.Next(range) + range) m_StackPanel.Add(item) End Sub Private Sub OnRemoveAllItemsButtonClick(args As NEventArgs) m_StackPanel.Clear() End Sub #End Region #Region "Fields" Private m_StackPanel As NStackPanel #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Stack Panel Example for WPF Solution

Related Documentation Links