Home > View Post

DataBinding in WPF rocks my world

I've been converting an old WinForms application of mine to WPF. It was quite graphically intensive with hundreds of lines of GDI+ so, being graphically rich, it was a natural candidate for conversion. First the downside - it can take a while to get familiar with WPF - the WTF of WPF, if you like. Now the upside. Once you do, it will blow you away. Seriously.

It doesn't take long working with WPF to realise that DataBinding is a core principle. I'd go as far to say that, if you're using WPF and you're not using DataBinding - you're almost certainly missing a trick. I know, DataBinding conjures up images of CRUD applications but it's capable of much much more.

In my case, the application in question creates complicated rendered shapes to visualise data. Not something you'd traditionally think of using DataBinding for but it's just a matter of creating a reasonable object model (which you may already have) and hooking it up in the right way. This is the bit that can take a bit of mastery but the results are well worth it. It's taken my hundreds of lines of GDI+ and left me with just one screenful of Xaml.

DataBinding in brief

If you're not familiar with DataBinding in WPF it looks something like this (see my other post for what the x:Static part is all about):

<Label Content="{Binding Source={x:Static sys:DateTime.Now}}" />

Not the most compelling use of DataBinding I admit - you could have just used a Static Resource. It would be more exciting if DateTime.Now published the fact that the Now property changes - but we can write our own class for that.

namespace TheJoyOfCode.WpfExample
{
    public class Ticker : INotifyPropertyChanged
    {
        public Ticker()
        {
            Timer timer = new Timer();
            timer.Interval = 1000; // 1 second updates
            timer.Elapsed += timer_Elapsed;
            timer.Start();
        }

        public DateTime Now
        {
            get { return DateTime.Now; }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Now"));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

The key to this is the implementation of the INotifyPropertyChanged interface. I won't bore you with the details - they should be pretty apparent or you can head off to MSDN to read all about it. What's important is that it works great with WPF. And here's how I'd use this class in Xaml:

<Page.Resources>
    <src:Ticker x:Key="ticker" />
</Page.Resources>
<Label Content="{Binding Source={StaticResource ticker}, Path=Now}"/>

Note how I construct an instance of the Ticker in the resources section. Neat.

More on databinding tomorrow.

Tags: WPF

 
Josh Post By Josh Twist
11:59 AM
19 Mar 2007

» Next Post: More than just dynamic updating
« Previous Post: Volunteer comes forward with hack for VS on Vista

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.