WPF Forms

New Project - WPF Application

Dialog Box

It is possible to use WPF in your Windows Forms solutions
Add a reference to "WindowsFormsIntegration"
Add a reference to "Presentation.Core"
Add a reference to "Presentation.Framework"
Add a reference to "Windows.Base"
Right click on your project and select (Add > New Item)
Choose "UserControl (WPF)" and provide a name of "UserControl1"

System.Windows.Controls.UserControl myusercontrol; 
myusercontrol = new UserControl1();
System.Windows.Window mywindow = new System.Windows.Window
   WindowStyle = System.Windows.WindowStyle.ToolWindow,
   Title = "my user control dialog",
   Content = myusercontrol,
   SizeToContent = System.Windows.SizeToContent.WidthAndHeight,
   ResizeMode = System.Windows.ResizeMode.CanResize,
mywindow.MinHeight = myusercontrol.MinHeight + 20;
mywindow.MinWidth = myusercontrol.MinWidth + 10;

The WPF Toolkit

Before a new control makes its way into the WPF libraries of the .NET platform, it typically starts off in the WPF Toolkit.
This toolkit is also a great source of practical components and controls that are not yet available in the normal WPF release cycle.
For example, the WPF Toolkit includes a set of controls for creating bar, pie, bubble, scatter, and line graphs.

Although you can craft WPF user interfaces by hand or using the graphic-design-oriented tool
Expression Blend, most developers will start in Visual Studio and spend most (or all) of their time there.

Naming Elements

There's one more detail to consider. In your code-behind class, you'll often want to manipulate controls programmatically
To make this possible, the control must include a XAML Name attribute.

Number = Grid.GetLeft(element) 
Grid.SetLeft(element, number)

Property Attributes

Property-Element Syntax

With property-element
syntax, you add a child element with a name in the form Parent.PropertyName. For example, the Grid
has a Background property that allows you to supply a brush that's used to paint the area behind the
controls. If you want to use a complex brush-one more advanced than a solid color fill-you'll need to
add a child tag named Grid.Background, as shown here:
<Grid Name="grid1">

Markup Extensions



static resource - this type will be resolved and assigned to the property during the loading of XAML
dynamic resource - bigger performance hit because it looks up the resources every time it is needed

Designer - design aids in the Document View window
Grid Rails - use this to manage rows and columns in a Grid control

Dependency Objects - contain certain additional control properties
These provide additional functionality over the standard properties

Custom Classes and Assemblies

Exposing public types


Defines the application and any application resources used to specify the UI which is automatically displayed when the application starts


The default window that is displayed

Tunnel Events (parent to child)

Also known as preview events
These are routed events which travel from the parent/application down to the element that raised the event

Bubble Events (child to parent)

These are routed events which travel from the element that raised the event up to the parent/application

WPF Units

The measurement system used is dpi (device independent units)

Class Hierarchy

Dispatcher Object
Dependency Object
Control - ContentControl / ItemsControl


A WPF window can only hold a single element
To fit more than one element you need to place a container in your window.

add a panel to the form and dock it
add component PF
create form
add reference to System.Xaml (.NET 4.0)
add reference to WindowsFormsIntegration

(Binding, TemplateBinding, x:Bind) - always use TemplateBinding Generic.xaml Visual States User Controls Dependency Properties

Public Class MyButton : Windows.UI.Xaml.Controls.Button { Public MyButton(): base() { This.DefaultStyleKey = typeof(Windows.UI.Xaml.Controls.Button);

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopNext