DCSIMG
Data Binding for Pure CLR Objects - Essential WPF

Data Binding for Pure CLR Objects

WPF has a great support for handling data: Data Binding. Unfortunately, one of the elements you bind to (the target) must have a DependencyProperty (known as the target-property).

What if you don't have a dependency property to bind to, or you just want to bind two CLR types?

For example let say that you wrote an application for simulating a car. You may have a Car and a Speedometer classes. Let say that a car handles its own speed, hence the car has its own speed state exposed by a Speed property. A speedometer measures speed, hence it also has its own speed state, thus exposes it with its own Speed property. Now you want to visualize both the Car and its Speedometer, each with its own state. So you wrote two Data Templates and you bind the data template elements with the Car.Speed and Speedometer.Speed properties.

image

Changing the car speed state doesn't change the speedometer speed state. You can work around this by registering to a Car.SpeedChange event, and handle it by updating the speedometer speed state. But what if you have more properties, more classes... You are right, it is a mess, unless you are using my little, simple CLR Binding class which will do the work for you.

image

 

        public Car()
        {
            this._speedometer = new Speedometer()
            {
                MinSpeed = 0,
                MaxSpeed = 240
            };
            CLRPropertyBinding binding = new CLRPropertyBinding(
                this,
                _speedometer,
                "Speed",
                CLRBindingMode.OneWayToTarget);
            binding.Bind();

            ...
        }

 

As you can see, instead of registering events, you only have to create an instance of the CLRPropertyBinding. This instance will do the rest.

Note that the CLRPropertyBinding class is neither completed nor perfect. It uses property descriptor (reflection) for updating both the target and the source. Also you can extend this type to support converters, error handling and so on.

Side note: this type is inspired by the WPF Binding markup extension class.

 

Before you download the code, read this. Also you are welcome to see more of this and other interesting Data Binding and Data Templates stuff in my session: "The Hitchhiker's Guide to WPF Data Binding", Tech-Ed Israel 2008.

You can download the complete code from here.

Published Wednesday, March 05, 2008 5:00 AM by Tomer Shamam

Comments

# re: Data Binding for Pure CLR Objects

Friday, March 28, 2008 4:20 AM by Erikitha

Thanks!, It helped me to understand better the CLR object.

Can you post an example how can I work with two tables (from the database) as a master detail and create and handle the CLR and display as two grids, the detail changing depending what you select on the master.

I have been looking for that example.

Thanks,

Your example have helped me a lot.

You are a good teacher, your explian clearly.

sorry, my english it is no to good.

bye,

# re: Data Binding for Pure CLR Objects

Friday, March 28, 2008 10:53 AM by Tomer Shamam

Hi Erikitha,

Thanks for your comment, you are a good reader :-)

As for your question, Master-Detail pattern in WPF is implemented such as you only have one collection-source (DataTable for example), presentng it using more than one view (one for the master, and other for details). Unfortunately, you can't choose more than one source.

One work around I can think of is to bind the source to the master view, displaying it in details using a value-converter to get the details from the other table.

For example:

<!-- Master View -->

<ListView

ItemsSource="{Binding Path=SourceProeprtyIfAny}"

IsSynchronizedWithCurrentItem="True" ... />

<!-- Details View -->

<!-- First Name -->

<TextBox Text="{Binding Path=\DETAILS_ID,

        Converter={StaticResource detailsConverter}}"

        ConverterParameter="FirstName"  ... />

<!-- Last Name -->

<TextBox Text="{Binding Path=\DETAILS_ID,

        Converter={StaticResource detailsConverter}}"

        ConverterParameter="LastName"  ... />

In the details view I'm using the path (\DETAILS_ID), which is the value in current row, DETAILS_ID column (which is a foreign key to the details table).

Where detailsConverter extracts the correct value from the details table using the ConverterParameter as the column name.

You can use a MultiBinding converter, to pass the details table as additional parameter (you may need it).

I will be glad to hear if it worked for you.

Tomer.

# re: Data Binding for Pure CLR Objects

Wednesday, January 28, 2009 4:45 PM by Dan Lamping

Here's an article with a good technique for binding to CLR properties.

<a href="www.wpfmentor.com/.../a>

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems