This article will explain Elements and their types in XAML.
Elements
- All XAML elements are an XML representation of CLR classes.
- Not all CLR classes have representations in XAML.
- XAML elements are usually derived from System.Windows.UIElement; they provide basic visual user interface capabilities. They can render themselves. They can receive input from the keyboard or mouse and can raise events.
- Not all XAML elements are derived from System.Windows.UIElement . Some are derived from System.Windows.FrameworkContentElement and System.Windows.Frame-workContentElement. Examples are LineBreak, TableColumn and document etc.
Root Elements
- They function as the page's base container for all user interface elements.
- A page is required to have one Root Element.
- To be considered as a Root Element, the element must be the container of at least one other element.
- Examples are Stack Panel, Dock Panel, Canvas, Grid and Page.
- A custom Root Element can be created by deriving from Page or Window and exposing them as an XAML element.
Control Element
- They handle user interactions.
- This Control is of 5 types. As follows:
- Simple Controls
- Content Controls
- Item Controls
- Header Item Controls
- Header Content Controls
Simple Controls
- Derived directly from System.Windows.Control
- They do not have Content, Items and Header attributes.
Example: HorizantleScrollBar, VerticalScrollBar, Frame, TextBox, RichTextBox etc.
Content Controls
- They do have Content attributes.
- They do not have Item and Header attributes.
Example: Button, RepearButton, Label, RadioButton, CheckBox, ListBox Item, GroupItem etc.
Item Controls
- They do have an Item attribute
- They do not have content and header attributes.
- They expose a list of elements, usually offering a choice.
Example: ListBox, ComboBox, Menu, ContextMenu,RadioButtonList,TabControl etc .
Header Item Control
- They do have an Item attribute.
- They do have a Header attribute.
- They do not have Content attribute.
Example: MenuItem etc.
Panel Elements
- Panel elements handle page layout and act as containers for elements like Controls or other panels.
- The primary purpose of the panel is to provide support for layout and placement of elements on the page.
- Some panel classes are intended for designing the user interface, while others are special panels designed specifically for special layout scenarios.
- The panel elements designed for user-interface design are DockPanel, StackPanel, Canvas, WrapPanel, and Grid.
Shape and Geometric Elements
- Shape and geometric elements represent 2-D vector graphics
- Shapes derive from the Shape class and represent predefined geometric shapes. WPF shapes available for use with XAML are Ellipse, Line, Path, Polygon, Polyline, and Rectangle
- Shapes are a type of UIElement, which means they can be used inside panels and most other controls
- Geometric elements, while also representing 2-D vector graphics, are more flexible than shape elements and can also be used for hit-testing and clipping purposes
- Geometry elements can be simple vector graphics, such as circles or polygons, or more complex elements comprised of Bezier lines and arcs
- Geometries cannot render themselves
- They must be drawn by another element, such as Drawing or Path
Document Elements
- They handle document presentation.
- They are of two types either Flow or Fixed.
- The Fixed Document element is designed to be what you see is what you get.
- A Flow Document provides more flexibility in appearance to enhance readability.
- Flow documents dynamically reformat content based on a variety of factors, including screen and page size, font size, and optional user preferences
- Flow documents are comprised of one or more elements derived from Block or Inline
- Block elements such as Block, Figure, Floater, List, ListItem, Paragraph, Section, Table, and TableCell are used to organize and format blocks of text.
- Inline elements are used to format text within a block. Inline elements are Bold, AccessKey, LineBreak, Hyperlink, Italic, Subscript, Superscript, and Underline
A button with three ellipses inside them. The following XAML does that:
<Window x:Class="Button.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<Button Width="100" Height="200">
<DockPanel>
<Ellipse Margin="10" DockPanel.Dock="Top" Stroke="Red" Fill="red" />
<Ellipse Margin="10" DockPanel.Dock="Top" Stroke="Black" />
<Ellipse Margin="10" DockPanel.Dock="Top" Stroke="Black" />
</DockPanel>
</Button>
</StackPanel>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<Button Width="100" Height="200">
<DockPanel>
<Ellipse Margin="10" DockPanel.Dock="Top" Stroke="Red" Fill="red" />
<Ellipse Margin="10" DockPanel.Dock="Top" Stroke="Black" />
<Ellipse Margin="10" DockPanel.Dock="Top" Stroke="Black" />
</DockPanel>
</Button>
</StackPanel>
</Window>