Wednesday, May 12, 2010

Converter

WPF binding binds one thing to another based on default conversion. For fancier conversion there is Converter. With Converter you can map more than one things to one thing.

User List in a Combo Box
  1.     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  2.     {
  3.         string name = string.Empty;
  4.         if (value is MyUser)
  5.         {
  6.             MyUser user = value as MyUser ;
  7.             name = string.Format("{0}{1}[{2}]", user.FirstName, user.LastName, user.ZipCode.ToString());
  8.         }
  9.         return name;
  10.     }… …<local:UserNameConverter x:Key="userNameConverter"/>
  11. <ComboBox>
  12.   <ComboBox.ItemTemplate>
  13.     <DataTemplate>
  14.       <StackPanel Orientation="Horizontal">
  15.         <TextBlock Text="{Binding Converter={StaticResource userNameConverter}}" />
  16.       </StackPanel>
  17.     </DataTemplate>
  18.   </ComboBox.ItemTemplate>
  19. </ComboBox>

 

 

Some Advanced Converters on Codeplex.com is here.

0 comments:

Post a Comment