Wednesday, September 22, 2010

XAML bind control to code-behind property

Say you want to bind, in Xaml, a control's property (ListView.ItemsSource) to a property defined in code-behind (MyItems), in a window or user control.

First, the code-behind property should be declared as a (normal) Propoerty or a DependecyProperty:

C#:


To bind, in Xaml, the ListView to this property, you need to:
XAML:

Monday, September 13, 2010

Apply the same style to all elements of a particular type

To apply the same style to all the elements of a particular type on a Window or UserControl, say all Labels, you need to specify the TargetType attribute of the Style:

All the labels on the Window will now have the properties specified in this style.

Sunday, September 12, 2010

Using system colors in WPF

What is the best way to use the system colors in a WPF application? Say you want to draw a rectangle with the background color set to one of the system colors, say the color of the active title bar. The main problem is that this color is not the same, it varies with the Windows theme used and might be different from one computer to another.

XAML: