WPF ListView Control

We build framework, you build application.

This should be the motto for Microsoft, and any framework provider. But thing is different in WPF world, try google how many people are working so hard to make ListView header click sortable.

I don’t like code behind, the classic event hook up isn’t a clean solution at all. There are a few smart guys using attached property to implement this sort feature, xaml can be much simpler, and we can slowly build our own WPF framewrok extension. (?)

The idea can be found from this post, but the attached source code are missing, this post uses same technology with full version of source code. In fact, I think Thomas’ solution is better, he is using a technique called Adorner, which is cleaner than the other one using switching template. The only part I don’t like is the extra GridViewSort.PropertyName in his xmal to set the sorting property name. Why not just use the native binding name? like this:

propertyName = ((System.Windows.Data.Binding)headerClicked.Column.DisplayMemberBinding).Path.Path;

That’s for click-header-sorting.

How about double-click to trigger event/command in view model? Check this post.

Is there an open source project somewhere called WPF contrib? Yes, check out the WPF tool kit on codeplex, at least the click-header-sorting is supported, including some other fancy feature, such as the alternate row color.


2 thoughts on “WPF ListView Control

  1. Hi Frank,

    I’m the author of the second post you mentioned. I explained in this post the reason why I didn’t use the DisplayMemberBinding property: basically, you can’t assume that this property will always be set. For instance, if you use a CellTemplate to define the content of a column, you don’t set the DisplayMemberBinding. You might also want to sort on a property which isn’t the one displayed on the column. For these reasons, I think the ability to explicitly define the sort property name for a column is more flexible than using the DisplayMemberBinding, even though I must admit it makes the code more verbose…

    Actually, it could be a good idea to fall back to the DisplayMemberBinding when the PropertyName attached property is not set, since most of the time it will be the desired behavior… I’ll try to update my code when I have the time.


    • That’s the reason why…

      I’m not very good at datatemplate/celltemplate. Less interesting for those UI magic… Maybe with more coding in WPF/xaml, my mind will change.

      Thanks for explaination.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s