Powered by Twitter Tools.

October 2008
M T W T F S S
« Sep   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  
Chris Donnan

Create Your Badge

Chris Donnan : Programming – Brooklyn Style

software, trading, family, fun

WPF Value Converter Fun

After a few months programming WPF daily (after years of daily WinForms) I am getting my groove. It is a mindset change I will admit – but one I am coming to like.

WPF is all about the binding. Tools for working with binding are very valueable. ValueConverters are one of the useful tools that every goo WPF’er should be adept at using.

The problem

A few weeks back I was working with the Xceed WPF grid. We had a multi-level heirarchical datagrid. I wanted to make it so that if a user right clicks on a row at the top level, they would get an appropriate context menu, but if they clicked on a sub-grid, they would get a different context menu. Being a lazy programmer I trolled the forums and google, but nobody had a good answer here – so I went on trying to find a solution.

The solution

Part one, the value converter:

Value converters are a pretty simple idea. When databinding, you have the opportunity to convert the value to/ from the binding source to the target.

Here is a picture:

So – the converter is the opportunity to ‘do stuff’ the bound values. My value converter’s job was to tell me if the thing I was selecting was of a specific type, so – I did just that – here is my trivial IsTypeOfConverter

Using it in XAML

In your XAML resources you can put a reference like this:

Basically what this is saying is – when you are styling the grid cell, dynamically set it’s context menu based on what type the underlying object is. Easy enough righ? Pretty direct mapping to the problem no?

More fun to come with attached properties and Markup Extensions..

-Chris

Share and Enjoy:
  • del.icio.us
  • digg
  • blinkbits
  • BlinkList
  • blogmarks
  • YahooMyWeb
  • connotea
  • De.lirio.us
  • Fark
  • Furl
  • Reddit
  • scuttle
  • Shadows
  • Smarking
  • Spurl
  • TailRank
  • Wists

Responses are currently closed, but you can trackback from your own site.