Namespace: System.Windows
Contains types that represent the core concepts of the Windows Presentation Foundation (WPF) and other Windows-related functionalities. This namespace is fundamental to building graphical user interfaces and managing window-based applications in the .NET Framework.
Summary of Core Concepts
The System.Windows
namespace provides the building blocks for Windows applications. It defines fundamental elements like windows, applications, events, and a powerful property system (Dependency Properties) that enables features like data binding, styling, and animation.
- UI Elements: While many UI elements are in sub-namespaces like
System.Windows.Controls
, the foundation is laid here. - Application Lifecycle: Manages the application's existence, message loop, and startup.
- Eventing Model: Provides the base for routed events and general event handling within WPF.
- Dependency Properties: A key feature that allows properties to have values from various sources (styles, templates, data binding) and provides built-in support for animation and change notification.
Window Class
Represents a top-level window, which is a container for other UI elements.
public class Window : ContentControl, IDisposable { ... }
The Window
class is the root element for most WPF applications. It provides the frame for the application's content, handles user input, and manages the window's state (size, position, visibility).
Properties
- Title:
string
- Gets or sets the text displayed in the window's title bar. - Width:
double
- Gets or sets the width of the window. - Height:
double
- Gets or sets the height of the window. - Content:
object
- Gets or sets the content of the window. - WindowState:
WindowState
- Gets or sets the current state of the window (Normal, Minimized, Maximized).
Events
- Activated:
RoutedEventHandler
- Occurs when the window is activated. - Closed:
EventArgs
- Occurs when the window is closed. - StateChanged:
EventArgs
- Occurs when the window's state changes.
Example
Creating and showing a simple window:
using System.Windows;
public class MyMainWindow : Window
{
public MyMainWindow()
{
this.Title = "My Application Window";
this.Width = 400;
this.Height = 300;
Button okButton = new Button();
okButton.Content = "OK";
okButton.Click += OkButton_Click;
this.Content = okButton;
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button clicked!");
}
[STAThread]
public static void Main()
{
MyMainWindow mainWindow = new MyMainWindow();
Application app = new Application();
app.Run(mainWindow);
}
}
Application Class
Represents the WPF application and provides properties and methods for managing application-level features.
public class Application : IWeakEventListener { ... }
The Application
class is the entry point for most WPF applications. It manages the application's message loop, resources, and the lifetime of its windows. You typically instantiate this class and call its Run()
method.
Properties
- Current:
Application
- Gets the currentApplication
instance. - MainWindow:
Window
- Gets or sets the main window of the application. - Windows:
ReadOnlyCollection<Window>
- Gets a collection of all windows that are currently open in the application.
Methods
- Run(Window window):
void
- Initializes a new instance of theApplication
class, creates a new message loop, and then starts the message loop and listens for incoming messages.
DependencyProperty Class
Represents a property that can be hosted by any dependency object.
public sealed class DependencyProperty { ... }
Dependency properties are a core feature of WPF. They extend the traditional .NET property system by allowing properties to have values derived from various sources, including data binding, styles, templates, and animations. They also provide built-in support for change notification and validation.
Key Concepts
- Property Metadata: Defines behavior like change notification callbacks and coercion functions.
- Value Precedence: Determines which value wins when a property is set from multiple sources.
- CoerceValueCallback: A callback that can modify a property value before it's set.
- PropertyChangedCallback: A callback that is invoked when a property's value changes.
Example (Conceptual Registration)
Registering a custom dependency property:
public static readonly DependencyProperty MyCustomProperty =
DependencyProperty.Register(
"MyCustom", // Property name
typeof(string), // Property type
typeof(MyDependencyObject), // Owner type
new FrameworkPropertyMetadata("Default Value") // Property metadata
);
public string MyCustom
{
get { return (string)GetValue(MyCustomProperty); }
set { SetValue(MyCustomProperty, value); }
}
EventArgs Class
Serves as the base class for classes that contain event data.
public class EventArgs { ... }
In the .NET Framework, event handlers typically follow a convention where they accept two arguments: an object sender and an EventArgs
object. The EventArgs
class itself is usually used when an event does not need to pass any specific data. Custom event arguments are created by deriving from EventArgs
.
Methods
- Empty:
EventArgs
- Gets an instance of theEventArgs
class that has no event data.
Visual Class
Represents an object that can render itself.
public abstract class Visual : DispatcherObject { ... }
The Visual
class is the base class for all WPF elements that have a visual representation but are not necessarily UI elements that interact directly with the user. This includes things like drawing primitives, effects, and composition nodes. It provides the fundamental rendering capabilities for WPF.
Methods
- RenderOpen():
DrawingContext
- Opens aDrawingContext
for drawing on the visual.