Using IValueConverter in Silverlight

So data binding really is a great thing, especially when it can go beyond the boundaries of just putting text or numbers into the required fields on a form.  I was talking with someone about my current project and they made the distinction between data and information, where information is data that had been converted into some useful form.  In essence, that's what the System.Windows.Data.IValueConverter is for in Silverlight and WPF; to take some piece of data and make it meaningful within the current context.

 

My recent example was from a program I wrote that involved the user uploading some files and then tagging them with the required metadata (Name, Caption for pictures, etc.).  I had a validator for saving the information so that I could give the user a warning if something was left incomplete, but I wanted it to be both easier to see and for it to be instantly responsive to changes in the data.  The goal was for user to see, at a glance, if all the current files were valid and, if not, which ones needed to be fixed.  So I started with the template for the Listbox containing my data files:

   1: <ListBox x:Name="PictureListBox">
   2:   <ListBox.ItemTemplate>
   3:     <DataTemplate>
   4:       <Grid>
   5:         <Grid.ColumnDefinitions>
   6:           <ColumnDefinition Width="104" />
   7:           <ColumnDefinition Width="*" />
   8:         </Grid.ColumnDefinitions>
   9:         <Rectangle Grid.Column="0" 
  10:           Fill="{Binding IsValid, Converter={StaticResource ValidationConverter}}" />
  11:         <Image Grid.Column="0" Height="100" Width="100" 
  12:           Source="{Binding Path}" />
  13:         <StackPanel Grid.Column="1" >
  14:           <... some other stuff that I'm binding... />
  15:         </StackPanel>
  16:       </Grid>
  17:     </DataTemplate>
  18:   </ListBox.ItemTemplate>
  19: </ListBox>

Each image in the listbox now has a rectangle behind it that will serve as a border.  I?m binding the fill property of that rectangle to the IsValid property on my data object.  Previously, I might have tried either binding this property to a separate object or, worse yet, including the proper color as another property in my data object.  Both of these could work for a small app like mine, but they don't feel right.  Hence the converter, which my xaml is made aware of like so:

   1: <UserControl.Resources>
   2:     <my:PictureValidationConverter x:Key="ValidationConverter" />
   3: </UserControl.Resources>

and finally the code for the converter:

   1: public class PictureValidationConverter:IValueConverter
   2:    {
   3:       public object Convert(object value, Type targetType, 
   4:          object parameter, System.Globalization.CultureInfo culture)
   5:       {
   6:          if ((bool)value)
   7:          {
   8:             return new SolidColorBrush(Colors.Green);
   9:          }
  10:          else
  11:          {
  12:             return new SolidColorBrush(Colors.Red);
  13:          }
  14:       }
  15:  
  16:       public object ConvertBack(object value, Type targetType, 
  17:          object parameter, System.Globalization.CultureInfo culture)
  18:       {
  19:          throw new NotImplementedException();
  20:       }
  21:    }
I actually modified the code to more cleanly fit here.  Using a ternary operator, it was a single line of code and all without touching the data object or having to go through some elaborate convulsions to avoid doing so.  The best part is that the update is instant; as soon as I've finished entering the data, the object can see if it's valid and the image border goes from red to green. 

Posted by: Raumornie
Posted on: 9/21/2009 at 1:38 PM
Tags: , ,
Categories: Software Kata
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (29) | Post RSSRSS comment feed

Comments

european gold United States

Monday, November 29, 2010 12:19 PM

european gold

One can assume you are an expert on this topic! I am unveiling a website soon, and your information will be very useful for me. Give Thanks for all your help and wishing you all the success in your business

instant payday loans United States

Friday, December 10, 2010 7:04 AM

instant payday loans

Hey! Wanted to go out of a comment. I really enjoyed reading this article. Sustain the awesome effort.

pay day loans United States

Friday, December 10, 2010 8:46 AM

pay day loans

Thanks so much for your downright post.this is actually the words that sustains me on course straight through out my day. I've been searching around just for this site after being referred to them from a colleague and was thrilled as i was able to locate it after seeking while. Being a demanding blogger, i’m hopeful to remarked further ones taking initivative and triggering town. True planned to comment showing my appreciation for the website since it is very intelligent to do, and several writers do not accumulate acknowledgment they deserve. I will be definite i’ll come back all of which will send most of my local freinds.

payday loans in georgia United States

Monday, December 13, 2010 4:50 PM

payday loans in georgia

Kudos for posting such a useful weblog. Your site isn’t only informative and also very artistic too. There are typically very few folks who can write not simple articles that creatively. Maintain the nice writing !!

cash advance loans United States

Monday, December 13, 2010 6:30 PM

cash advance loans

Thank you for taking a few minutes to talk about this, I find myself strongly regarding it and love learning more about this topic. Whenever possible, because you gain expertise, could you mind updating your blog with extra information? It is very helpful for me.

ggggggg United States

Monday, December 13, 2010 8:36 PM

ggggggg

trick or treat!

no fax no teletrack payday loans in Arizona Malta

Tuesday, December 14, 2010 4:45 AM

no fax no teletrack payday loans in Arizona

pay day loan United States

Tuesday, December 14, 2010 11:19 AM

pay day loan

Many thanks making the effort go over this, I'm strongly about this and love learning more on this topic. Whenever possible, while you gain expertise, could you mind updating your site with extra information? It is rather helpful for me.

pay day loan United States

Tuesday, December 14, 2010 12:52 PM

pay day loan

Hello from Russia! Would you like feasible for me to quote a submit with your weblog using the connect to you? I’ve tried contacting you with this problem but it seems i cant reach you, please response when use a moment, thanks.

Yuko Gadlin United States

Tuesday, December 14, 2010 8:17 PM

Yuko Gadlin

What i find difficult is to find a blog that can capture me for a minute but you definitely add value. Keep it like this.

fast payday loans in Michigan Kyrgyzstan

Tuesday, December 14, 2010 9:49 PM

fast payday loans in Michigan

n most occassions I don't post comments on blogs, but I want to say that this post really inspired me to do so. Really useful post

cash payday loans in Tennessee bd

Tuesday, December 14, 2010 10:45 PM

cash payday loans in Tennessee

The blog is nice one. I like the design of the website

one hour advance payday loans for benefits United Kingdom

Wednesday, December 15, 2010 12:57 AM

one hour advance payday loans for benefits

Big Thanks for the post Added to bookmarks permanently

hassle free payday loans in Delaware Spain

Wednesday, December 15, 2010 3:06 AM

hassle free payday loans in Delaware

excellent read. Added to bookmarks

advance america payday Kuwait

Wednesday, December 15, 2010 3:27 AM

advance america payday

excellent post, I just gave link to it to a friend, he performing a little amount of investigation on this subject

online payday loans no fax in Utah Ukraine

Wednesday, December 15, 2010 9:43 AM

online payday loans no fax in Utah

very good info, thx

check city payday loans Hungary

Wednesday, December 15, 2010 2:37 PM

check city payday loans

n most occassions I don't make comments on articles, but I would like to say that this article really inspired me to do it. Amazingly useful info

Yasuko Spidell United States

Thursday, December 16, 2010 11:32 PM

Yasuko Spidell

You may have not intended to do so, but I think you have managed to express the state of mind that a lot of people are in. The sense of wanting to help, but not knowing how or where, is something a lot of us are going through.

american cash advance Singapore

Thursday, December 16, 2010 11:35 PM

american cash advance

excellent post, I just gave link to it to a co-worker, he was doing a little amount of investigation on this subject

unsecured personal loans with no credit check Honduras

Friday, December 17, 2010 12:18 AM

unsecured personal loans with no credit check

no employment verification payday loans in Illinois Bolivia

Friday, December 17, 2010 1:30 AM

no employment verification payday loans in Illinois

The website is good one. I like the design of the website

paperless payday loans in Texas Caribbean

Friday, December 17, 2010 3:55 AM

paperless payday loans in Texas

very good info, thank you

nexus pheromones scam United States

Friday, December 17, 2010 8:40 AM

nexus pheromones scam

Recently i noticed your blog so are already reading along. We figured We leave my first comment. I don’t extremely well to apart from I’ve enjoyed reading. Nice blog. Steer clear of keep visiting this site really often.

Online Football Betting Euro2012 United States

Friday, December 17, 2010 11:18 PM

Online Football Betting Euro2012

You're not the general blog author, man.  You surely have something powerful to contribute to the web.  Such a special blog.  I will come back again for more.

Boyle Sports Promotional Code United States

Saturday, December 18, 2010 4:53 AM

Boyle Sports Promotional Code

A well written share, I just passed this onto a university student who was doing a little research on that. And he in fact purchased me breakfast because I found it for him.. smile. So let me reword that: Thankx for the treat! But yeah Thankx for taking the time to discuss this, I feel strongly about it and enjoy reading more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me. Big thumb up for this blog post!

Partybet Promotional Code United States

Saturday, December 18, 2010 3:15 PM

Partybet Promotional Code

It is rare for me to find something on the internet that's as entertaining and intriguing as what you've got here.  Your page is lovely, your graphics are outstanding, and what's more, you use reference that are relevant to what you are saying.  You are certainly one in a million, good job!

dog grooming United States

Saturday, December 18, 2010 4:05 PM

dog grooming

I like reading this site but I can't get it to load on my phone when I am at work, any ideas on how to fix that?

phentermine adipex United States

Sunday, December 19, 2010 7:25 AM

phentermine adipex

Finally, an issue that I am passionate about. I've appeared for info of this caliber for the final several hours. Your webwebsite is significantly appreciated.

Lamborghini United States

Sunday, December 19, 2010 9:53 AM

Lamborghini

Thank you for another fantastic blog. Where else could one get that kind of information written in such a perfect way? I have a presentation that I am presently working on, and I have been looking for such info instead.

Comments are closed