WPF UI APIs
This page provides an overview of the core Windows Presentation Foundation (WPF) APIs related to user interface elements, controls, layout, and presentation.
System.Windows.Controls
This namespace is central to WPF development, housing a rich collection of pre-built UI elements that can be used to construct application interfaces.
Common Controls:
-
Button: Represents a clickable button.
Properties: Content, Command, IsEnabled, Background, Foreground.
Events: Click.
-
TextBlock: Displays plain text.
Properties: Text, FontSize, FontWeight, FontFamily, Foreground.
-
TextBox: Allows user text input.
Properties: Text, IsReadOnly, AcceptsReturn, MaxLength.
Events: TextChanged.
-
Label: Displays a caption for another control.
Properties: Content.
-
Image: Displays image content.
Properties: Source, Stretch.
-
ScrollViewer: Provides scrolling functionality.
Properties: HorizontalScrollBarVisibility, VerticalScrollBarVisibility.
Items Controls:
These controls are designed to display collections of data.
-
ListBox: A control that displays a list of items.
Properties: Items, SelectedItem, SelectionMode.
Events: SelectionChanged.
-
ListView: Displays data in a flexible list format.
Properties: Items, View.
-
TreeView: Displays hierarchical data.
Properties: Items, ItemsSource.
System.Windows.Layout
Layout management is crucial for responsive and well-organized user interfaces. These classes control how elements are positioned and sized.
-
Grid: Arranges elements in rows and columns.
Properties: RowDefinitions, ColumnDefinitions. Use Row, Column attached properties on child elements.
-
StackPanel: Arranges elements in a single line (horizontal or vertical).
Properties: Orientation.
-
DockPanel: Arranges elements by docking them to the top, bottom, left, or right of the panel.
Properties: Dock attached property on child elements.
-
Canvas: Provides an absolute positioning system.
Properties: Use attached properties like Canvas.Left and Canvas.Top.
-
Border: Applies a border, background, or padding to a single child element.
Properties: BorderBrush, BorderThickness, Padding, Background.
This namespace provides the foundation for graphics, drawing, visual effects, transformations, and more.
-
Brush: An abstract class for filling shapes, backgrounds, etc.
Concrete classes include SolidColorBrush, LinearGradientBrush, RadialGradientBrush, ImageBrush.
-
Pen: Defines how lines and outlines are drawn.
Properties: Brush, Thickness.
-
Transform: Abstract class for geometric transformations.
Concrete classes include TranslateTransform, RotateTransform, ScaleTransform, SkewTransform.
System.Windows.Shapes
Defines a set of basic geometric shapes that can be used as controls or visual elements.
-
Rectangle: A rectangular shape.
Properties: Fill, Stroke, StrokeThickness, RadiusX, RadiusY.
-
Ellipse: An elliptical shape.
Properties: Fill, Stroke, StrokeThickness.
-
Line: A single straight line segment.
Properties: X1, Y1, X2, Y2, Stroke, StrokeThickness.
-
Path: Defines a complex shape using geometric path data.
Properties: Data (Geometry object), Fill, Stroke.
Handles user input such as mouse clicks, keyboard presses, and touch events.
-
RoutedEvent: A mechanism for communication between elements.
Events are often routed through the element tree.
-
ICommand: Interface for command pattern implementation, used for actions.
Common implementations: RoutedCommand, DelegateCommand.
-
MouseEventArgs: Provides data for mouse events.
Properties: ClickCount, ButtonState, MouseDevice.
-
KeyEventArgs: Provides data for keyboard events.
Properties: Key, KeyStates.
Example Usage: Creating a Button with TextBlock
This XAML demonstrates how to define a simple button with text content.
<Button Content="Click Me" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button.ToolTip>
<TextBlock Text="This is a tooltip!"></TextBlock>
</Button.ToolTip>
</Button>
Example Usage: Arranging Controls in a Grid
This XAML shows how to use a Grid layout panel to arrange elements.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Name:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="5"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="5"/>
<Button Content="Submit" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="10"/>
</Grid>