RSS
Facebook
Twitter

Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Thursday, April 01, 2010

How to data bind to property in WPF

One of the strengths of WPF is its data binding capabilities. Although data binding is not new (in fact winforms has some limited data binding support) WPF takes it to the next level. In this post I’ll show you how to bind an element to a property defined in the code behind.

How it’s done?

In order to bind a property to an element on the control we need to do the following:

  1. Create a dependency property
  2. Name your user control – in XAML
  3. Bind the property to an element of your choice

Step 1 – Create a dependency property

Dependency properties are simple .NET properties done the WPF way. On the surface they look just like the properties we use since .NET 1.x came out, underneath they are actually stored in a dictionary like collection and that implementation is what makes WPF work the way it does.

You don’t need to actually know how dependency properties work in order to create them but if you do want to learn more there is a good overview on MSDN.

Adding dependency property is done by creating a new field and initializing it using DependencyProperty.Register and then using that field in the property’s setter and getter:

public partial class Window1 : Window

{

    public static DependencyProperty SomeTextProperty =

        DependencyProperty.Register("SomeText", typeof(string), typeof(Window1));

 

    public string SomeText

    {

        get { return (string)GetValue(SomeTextProperty); }

        set { SetValue(SomeTextProperty, value); }

    }

Make sure that the name of the property used to register the property is exactly the same as the property name- otherwise it won’t work.

We’re done with the “code” part – now let’s head to the XAML and create the actual data binding.

Step 2 – Define the control’s name

In order to use the property we need to address it and for that we need to define a name for our control. Choose a name and add Name=<user control name> at the beginning of the XAML file. It doesn't matter how you call the control as long as you use the same name in the actual binding.

Step 3 – Bind the property to an element

In this trivial example I’ve used the property to set (and get) the text that appears on a textbox – and the XAML should look something like this:

<Window x:Class="BindToProperty.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" Name="UserControlName">

    <Grid>

        <TextBox Name="textBox1" Text="{Binding ElementName=UserControlName, Path=SomeText}"/>

    </Grid>

</Window>

 

And now whenever you change the value of Window1.SomeText that text will be displayed on the textbox “automatically”




Wednesday, December 09, 2009

Book Review: WPF in Action with Visual Studio 2008

Introduction

I was a bit worried when I first saw this book title – using visual studio version seems a bit over specified for my taste, reading the sub title made me even queasier - It looked as if the book will become obsolete in just a few months when VS2010 will be released. Today after reading WPF in Action I know that I will keep using it as a reference for many more years.

This book is good for WPF beginners that want to learn how to make beautiful (and cool) user interface (UI) as well as more advanced programmers that need to be reminded how to create a specific effect.

I especially liked the casual tone the authors use along with funny (geeky) jokes and anecdotes, this book feels more as if the authors are having an informal conversation with you then a technical book.

This book explain why things were created the way they are when possible or explain why the alternative is much worse in some cases.

WPF in Action’s 520 pages cover as much of WPF as humanly possible, although I think that more information about using blend is missing, but then one could argue that it would make the book so heavy that you might loose a toe if the book fell on it. Besides this book title suggests that it would be centered on VS2008 and not expression blend.

The book is divided into four parts that gradually bring you up to speed with WPF development (with VS2008 :) ).

Part 1 - Past, present, and future

This part begins with a bit of history – about windows API, MFC and webforms and how they evolved into WPF. Although this chapter is not crucial it’s a good summery of windows UI development.

Next we have the inevitable “hello world” application – at least it seems that the authors detest “hello world” examples as much as I do. After that there is a short explanation about the development environment (you guessed it VS2008) and a short overview of WPF (WPF from 723 feet).

Part 2 - The basics

This part we get to see WPF in action. While creating a calculator application we’re introduced to the layout system, events, styles, templates and the new way property behave in WPF. After reading this chapter you can start writing your first WPF based UI. Part 2 ends with the cool chapter 8 that gives a glimpse to how WPF can be used to create really cool controls and effects (as well as a working calculator).

Part 3 – Application development

After we’ve finished the simple calculator in the previous part this part shows how to create applications with complex UI layout start with a Wiki. Commands and routing are introduced as well as the all powerful data binding.

From there we continue to template, valuators and the M-V-VM Uber-pattern.

I guess because data binding while important can be a bit boring this part ends with three chapters about custom controls (make me a button with animation on it), and drawing (2D and 3D) that shows exactly where WPF “shines”.

Part 4 – The last mile

This is where the “advanced topics” are – navigation application, WPF and Silverlight and printing to name a few.

I especially liked the chapters about transition effects – because in my opinion this is what WPF is all about.

The last two chapters are about interoperability and threading – two “real world” necessities that I’m glad that the authors found important enough to cover in this book.

Conclusion

Finding a good book to learn the huge topic of WPF is no simple task, while many books on the subject insist on showing you how things look in both XAML and code WPF in action prefers to show you how to do a specific job.

As a past MFC to winforms developers I enjoyed reading about the WPF internals that are explained throughout this book.

If you’re a .NET developer new to the WPF world looking for a good book I recommend you read it. I know that although I’ve finished this book I’m still using it as a reference daily.

Related Posts Plugin for WordPress, Blogger...