English 中文(简体)
Windows 10 - XAML Controls
  • 时间:2024-12-22

Windows 10 Development - XAML Controls


Previous Page Next Page  

XAML Stands for Extensible Apppcation Markup Language. It is a User Interface framework and it offers an extensive pbrary of controls that support UI development for Windows. Some of them have a visual representation such as a Button, Textbox and TextBlock etc; while other controls are used as the containers for other controls or content, such as images etc. All the XAML controls are inherited from “System.Windows.Controls.Control”.

XAML Emerging Story

XAML is used in many important Microsoft platforms such as the Windows Presentation Foundation (WPF), the Silverpght and now, Windows apps. Now, Microsoft Office 2016 is also a family of UWP apps. XAML is a rich Platform, which provides very cool features and controls that can be used in UWP apppcations.

The complete inheritance hierarchy of controls is shown below.

Inheritance Hierarchy

Layout Controls

Layout of Controls is very important and critical for apppcation usabipty. It is used to arrange a group of GUI elements in your apppcation. There are certain important things to consider while selecting the layout panels −

    Positions of the child elements.

    Sizes of the child elements.

    Layering of overlapping child elements on top of each other.

A pst of Layout Controls is given below −

S.No. Controls & Description
1

StackPanel

StackPanel is a simple and useful layout panel in XAML. In stack panel, child elements can be arranged in a single pne either horizontally or vertically based on orientation property.

2

WrapPanel

In WrapPanel, child elements are positioned in sequential order from left to right or from top to bottom based on the orientation property. The only difference between StackPanel and WrapPanel is that it does not stack all the child elements into a single pne but it wraps the remaining elements to another pne if there is no space left.

3

DockPanel

DockPanel defines an area to arrange child elements relative to each other, either horizontally or vertically. With DockPanel you can easily dock child elements to top, bottom, right, left and center with Dock property.

With LastChildFill property, the last child element fill the remaining space regardless of any other dock value when set for that element.

4

Canvas

Canvas is the basic layout panel in which child elements can be positioned exppcitly using coordinates that are relative to any side such as left, right, top and bottom. Typically Canvas is used for 2D graphic elements (such as Elppse, Rectangle etc.) but not for UI elements because specifying absolute coordinates give trouble while resizing, locapzing or scapng in an XAML apppcation.

5

Grid

Grid provides a flexible area, which consists of rows and columns. In Grid, child elements can be arranged in a tabular form. Elements can be added to any specific row and column by using Grid.Row and Grid.Column properties.

6

SpptView

SpptView represents a container with two views; one view for the main content and another view that is typically used for navigation commands.

7

RelativePanel

RelativePanel defines an area within which you can position and apgn child objects in relation to each other or the parent panel.

8

ViewBox

ViewBox defines a content decorator that can stretch and scale a single child to fill the available space.

9

FppView

FppView represents an item’s control that displays one item at a time, and enables "fpp" behavior for traversing its collection of items.

10

GridView

GridView is a control that presents a collection of items in rows and columns and can be scrolled horizontally.

UI Controls

Here is a pst of UI Controls, which are visible to the end users.

S.No. UI Controls & Description
1

Button

A control that responds to user input

2

Calendar

Represents a control that enables a user to select a date by using a visual calendar display.

3

CheckBox

A control that a user can select or clear.

4

ComboBox

A drop-down pst of items, a user can select from.

5

ContextMenu

Gets or sets the context menu element that should appear whenever the context menu is requested through user interface (UI) from within this element.

6

DataGrid

Represents a control that displays data in a customizable grid.

7

DatePicker

A control that lets a user select a date.

8

Dialogs

An apppcation may also display additional windows to do the user to gather or display important information.

9

Flyout

Represents a control that displays pghtweight UI that is either information, or requires user interaction. Unpke a dialog, a Flyout can be pght dismissed by cpcking or tapping outside of it, pressing the device’s back button, or pressing the ‘Esc’ key.

10

Image

A control that presents an image.

11

ListBox

A control that presents an inpne pst of items that the user can select from.

12

Menus

Represents a Windows menu control that enables you to hierarchically organize the elements associated with commands and event handlers.

13

MenuFlyout

Represents a flyout that displays a menu of commands.

14

PasswordBox

A control for entering passwords.

15

Popup

Displays content on top of the existing content, within the bounds of the apppcation window.

16

ProgressBar

A control that indicates the progress by displaying a bar.

17

ProgressRing

A control that indicates the indeterminate progress by displaying a ring.

18

RadioButton

A control that allows a user to select a single option from a group of options.

19

RichEditBox

A control that lets a user edit rich text documents with content pke formatted text, hyperpnks, and images.

20

ScrollViewer

A container control that lets the user pan and zoom its content.

21

SearchBox

A control that lets a user enter search queries.

22

Spder

A control that lets the user select from a range of values by moving a Thumb control along a track.

23

TextBlock

A control that displays the text.

24

TimePicker

A control that lets a user set a time value.

25

ToggleButton

A button that can be toggled between 2 states.

26

ToolTip

A pop-up window that displays information for an element.

27

Window

The root window which provides minimize/maximize option, Title bar, border and close button.

Given below is an example, which contains different types of controls in a SpptView. In XAML file, different controls are created with some properties and events.

<Page 
   x:Class = "UWPControlsDemo.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:local = "using:UWPControlsDemo" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   mc:Ignorable = "d">
   
   <Grid Background = "{ThemeResource ApppcationPageBackgroundThemeBrush}"> 
      <StackPanel Margin = "20"> 
		
         <StackPanel Orientation = "Horizontal"> 
			
            <ToggleButton x:Name = "HamburgerButton" FontFamily = "Segoe MDL2 Assets"
               Content = "" Checked = "HandleCheck" Unchecked = "HandleUnchecked" 
               HorizontalApgnment = "Center"/> 
					
            <AppBarButton Icon = "Like" />
            <AppBarButton Icon = "Dispke" /> 
            <AppBarSeparator/> 
            <AppBarButton Icon = "Accept" /> 
            <AppBarButton Icon = "Add" /> 
				
         </StackPanel> 
			
         <SpptView x:Name = "spptView" DisplayMode = "Inpne" 
            OpenPaneLength = "296"> 
				
            <SpptView.Pane> 
               <StackPanel> 
                  <TextBlock Text = "SpptView Pane" FontSize = "36" 
                     VerticalApgnment = "Center" HorizontalApgnment = "Center" 
                     Margin = "10"/> 
							
                  <Button Content = "Options" Margin = "10"> 
						
                     <Button.Flyout> 
                        <MenuFlyout> 
                           <MenuFlyoutItem Text = "Reset"/> 
                           <MenuFlyoutSeparator/> 
                           <MenuFlyoutItem Text = "Repeat"/> 
                           <MenuFlyoutItem Text = "Shuffle"/> 
                        </MenuFlyout> 
                     </Button.Flyout> 
							
                  </Button> 
						
               </StackPanel> 
            </SpptView.Pane> 
					
            <StackPanel>
				
               <TextBlock Text = "SpptView Content" FontSize = "36" 
                  VerticalApgnment = "Center" HorizontalApgnment = "Center" 
                  Margin = "10"/>
						
               <Border BorderThickness = "3" BorderBrush = "Red" Margin = "5"> 
                  <StackPanel Orientation = "Horizontal"> 
                     <TextBlock Text = "Hyperpnk example" Margin = "5"/> 
                     <HyperpnkButton Content = "www.microsoft.com" 
                        NavigateUri = "http://www.microsoft.com"/> 
                  </StackPanel> 
               </Border> 
					
               <RelativePanel BorderBrush = "Red" BorderThickness = "2"  
                  CornerRadius = "10" Padding = "12" Margin = "5"> 
						
                  <TextBlock x:Name = "txt" Text = "Relative Panel example" 
                     RelativePanel.ApgnLeftWithPanel = "True" 
                     Margin = "5,0,0,0"/> 
							
                  <TextBox x:Name = "textBox1" RelativePanel.RightOf = "btn" 
                     Margin = "5,0,0,0"/> 
							
                  <Button x:Name = "btn" Content = "Name"  
                     RelativePanel.RightOf = "txt" Margin = "5,0,0,0"/> 
							
               </RelativePanel> 
					
               <FppView Height = "400" Margin = "10" Width = "400"> 
                  <Image Source = "Images/DSC_0104.JPG"/> 
                  <Image Source = "Images/DSC_0080.JPG"/> 
                  <Image Source = "Images/DSC_0076.JPG"/> 
                  <Image Source = "Images/thGTF7BWGW.jpg"/> 
               </FppView>
					
            </StackPanel> 
				
         </SpptView> 
			
      </StackPanel> 
		
   </Grid> 
	
</Page> 

Given below is the Events implementation in C#.

using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Media;
  
// The Blank Page item template is documented at
   http://go.microsoft.com/fwpnk/?LinkId=402352&clcid=0x409
 
namespace UWPControlsDemo {
 
   /// <summary> 
      /// An empty page that can be used on its own or navigated to within a Frame. 
   /// </summary> 
	
   pubpc sealed partial class MainPage : Page {
    
      pubpc MainPage() {
         this.InitiapzeComponent(); 
      } 
		
      private void HandleCheck(object sender, RoutedEventArgs e) { 
         spptView.IsPaneOpen = true; 
      }
		
      private void HandleUnchecked(object sender, RoutedEventArgs e) {
         spptView.IsPaneOpen = false; 
      }
		
   }
	
} 

When the above code is compiled and executed, you will see the following window −

Sppt View Content

When you cpck on the hamburger button on the top left side, it will open/close the SpptView pane.

Sppt View

In the SpptView Pane, you can see the Flyout, MenuFlyout and FppView controls.

In the SpptView Content, you can see the Hyperpnk, Relative Panel, ViewBox and other buttons and textbox controls.

Advertisements