Learn Universal Windows Programming Via Modern C++ (Stackpanel)

Before reading this article, I highly recommend reading the previous part of the series.

Stackpanel

Stackpanel is merely holding one or more controls into a panel. The inside panel controls are called child controls. Child controls can be arranged into horizontal (left to right) or vertical (top to bottom) stacks. We can add all the itemcontrols into the Stackpanel, like textbox, button, and combobox.

Why is Stackpanel holding more controls? One of the reasons are window objects, as you can see in the Button control sample coding the window object is holding only one control at a time. Yet if we add another control the first control gets overwritten.

Sample of two controls into the window object: 

  1. void App::OnLaunched(LaunchActivatedEventArgsconst &)   
  2. {  
  3.     Button BtnClick;  
  4.     IInspectable captionText = PropertyValue::CreateString(L "Click Me");  
  5.     BtnClick.Content(captionText);  
  6.     //To change background and Foreground of the color  
  7.     BtnClick.Background(SolidColorBrush(Colors::Red()));  
  8.     BtnClick.Foreground(SolidColorBrush((Colors::Yellow())));  
  9.     BtnClick.Click( & App::BtnClick);  
  10.     Button BtnClickOK;  
  11.     IInspectable captionOK = PropertyValue::CreateString(L "OK");  
  12.     BtnClickOK.Content(captionOK);  
  13.     Window window = Window::Current();  
  14.     window.Content(BtnClick);  
  15.     window.Content(BtnClickOK);  
  16.     window.Activate();  
  17. }  
Button control has overwritten output

 

 

To avoid this, stackpanel will be very helpful. Add all the controls into the stackpanel, lastly add the stackpanel control into the window object so that all the controls are visible. Let see the stackpanel properties with an example.

Properties

Children

All the itemcontrols have to be added into the children property. Children is a UIElementCollection container, by default all the controls are added into the zero-index order. Append properties is used to add the controls into the container.

Example

  1. stPanel.Children().Append(CreateTextBlock(L"Panel 1"));  
  2. stPanel.Children().Append(CreateTextBlock(L"Panel 2"));  
Insert properties is used to place the control into the index position. (breaking the zero-index concept)

 

Example

  1. stPanel.Children().InsertAt(4,CreateTextBlock(L"Panel 5"));  
  2. stPanel.Children().InsertAt(5,CreateTextBlock(L"Panel 6"));  

 

Orientation

Orientation is used to arrange the child controls vertically or horizontally, by default the stackpanel orientation is vertical. 

Vertical (top to bottom)

 

Horizontal (left to right)

 

Nested Stackpanel

Stackpanel is added inside another stackpanel...

 

Let's see one sample application on how to use Orientation property and nested stackpanels.

  1. #include "pch.h"  
  2. using namespace winrt;  
  3. using namespace Windows::ApplicationModel;  
  4. using namespace Windows::ApplicationModel::Activation;  
  5. using namespace Windows::Foundation;  
  6. using namespace Windows::UI;  
  7. using namespace Windows::UI::Xaml;  
  8. using namespace Windows::UI::Xaml::Controls;  
  9. using namespace Windows::UI::Xaml::Controls::Primitives;  
  10. using namespace Windows::UI::Xaml::Interop;  
  11. using namespace Windows::UI::Xaml::Media;  
  12. using namespace Windows::UI::Xaml::Navigation;  
  13. using namespace Windows::UI::Popups;  
  14. using namespace Windows::Media::Ocr;  
  15. using namespace Windows::Storage;  
  16. struct App: ApplicationT < App > {  
  17.     public: virtual~App() =  
  18.         default;  
  19.     static TextBlock CreateTextBlock(hstring);  
  20.     static StackPanel CreateStackPanel(hstring, Orientation);  
  21.     static void OnLaunched(LaunchActivatedEventArgs  
  22.         const & );  
  23.     static Thickness CreateThickness(int bottom, int left, int right, int top);  
  24. };  
  25. Thickness App::CreateThickness(int bottom, int left, int right, int top) {  
  26.     Thickness think;  
  27.     think.Bottom = bottom;  
  28.     think.Left = left;  
  29.     think.Right = right;  
  30.     think.Top = top;  
  31.     return think;  
  32. }  
  33. TextBlock App::CreateTextBlock(hstring textCaption) {  
  34.     TextBlock text;  
  35.     text.Text(textCaption);  
  36.     text.TextAlignment(TextAlignment::Center);  
  37.     text.Margin(CreateThickness(10, 10, 0, 10));  
  38.     return text;  
  39. }  
  40. StackPanel App::CreateStackPanel(hstring panelName, Orientation oriPosition) {  
  41.     StackPanel stPanel;  
  42.     stPanel.Children().Append(CreateTextBlock(panelName));  
  43.     stPanel.Children().Append(CreateTextBlock(L "Panel 1"));  
  44.     stPanel.Children().Append(CreateTextBlock(L "Panel 2"));  
  45.     stPanel.Children().Append(CreateTextBlock(L "Panel 3"));  
  46.     stPanel.Children().Append(CreateTextBlock(L "Panel 4"));  
  47.     stPanel.Children().InsertAt(4, CreateTextBlock(L "Panel 5"));  
  48.     stPanel.Children().InsertAt(5, CreateTextBlock(L "Panel 6"));  
  49.     stPanel.Orientation(oriPosition);  
  50.     if (oriPosition == Orientation::Horizontal) stPanel.Background(SolidColorBrush(Colors::Red()));  
  51.     else stPanel.Background(SolidColorBrush(Colors::Blue()));  
  52.     return stPanel;  
  53. }  
  54. void App::OnLaunched(LaunchActivatedEventArgs  
  55.     const & ) {  
  56.     auto panel1 = CreateStackPanel(L "StackPanel 1", Orientation::Horizontal);  
  57.     auto panel2 = CreateStackPanel(L "StackPanel 2", Orientation::Vertical);  
  58.     StackPanel panel;  
  59.     panel.Children().Append(panel1);  
  60.     panel.Children().Append(panel2);  
  61.     panel.BorderThickness(CreateThickness(5, 5, 5, 5));  
  62.     panel.Background(SolidColorBrush(Colors::Yellow()));  
  63.     Window window = Window::Current();  
  64.     window.Content(panel);  
  65.     window.Activate();  
  66. }  
  67. int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {  
  68.     Application::Start([](auto && ) {  
  69.         make < App > ();  
  70.     });  
  71.     return 0;  
  72. }  

Output 

 

Conclusion

I hope you understood how to use the stackpanel controls. 


Similar Articles