IValueConverter: The Swiss Army Knife of Bindings [PropertyViewer sample is a WPF/Silverlight visualization and debugging aid!]


This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

PropertyViewer.zip

Comments (14)
  1. vgsbs says:

    Cool – I really appreciate you do everything for both Silverlight and WPF.

  2. Apologies for the sparseness of my posting the last few weeks – work and life have been busy here lately

  3. Apologies for the sparseness of my posting the last few weeks – work and life have been busy here lately. 

  4. Apologies for the sparseness of my posting the last few weeks – work and life have been busy here lately

  5. Mijn verontschuldigingen voor de weinige posts de laatste weken. Mijn werk en leven zijn enorm de druk

  6. Mes excuses pour le peu de publications au cours des dernières semaines – le travail et la vie en générale

  7. Apologies for the sparseness of my posting the last few weeks – work and life have been busy here lately

  8. Miguel Madero says:

    It’s a great example.

    Just as a comment, I like the way Ruby On Rails simplify the binding to objects, letting you get friendly names using the Humanize method. That way using an attribute as [DisplayName("Eats Bugs")] is not need just to split the PropertyName based on the case. Altough the attribute gives you more control.

    I can see how IValueConverter can be useful in certain escenarios, but most of the time using a FormString would be easier.

    I dont know if there’s something like that in SL or WPF. I did a trick to do something similar using ValueConverters, it work fine, but I think there should be a sexier solution.

    Here’s the XAML.

    <UserControl.Resources>

          <local:StringFormatConverter x:Name="stringFormatter"/>

      </UserControl.Resources>

      <StackPanel Orientation="Vertical">

          <StackPanel.DataContext>

              <SilverlightApplication3:Person/>

          </StackPanel.DataContext>

          <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding Mode=OneWay, Path=Name}" TextWrapping="Wrap"/>

          <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding Mode=OneWay, Path=LastName}" TextWrapping="Wrap"/>

          <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text='{Binding Mode=OneWay, Path=DOB, Converter={StaticResource stringFormatter}, ConverterParameter="d"}’ TextWrapping="Wrap"/>

          <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text='{Binding Mode=OneWay, Path=Age, Converter={StaticResource stringFormatter}, ConverterParameter="c"}’ TextWrapping="Wrap"/>

          <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text='{Binding Mode=OneWay, Path=AnualIncome, Converter={StaticResource stringFormatter}, ConverterParameter="0.00"}’ TextWrapping="Wrap"/>

      </StackPanel>

    Here’s de Code

    public class StringFormatConverter:IValueConverter

      {

          #region IValueConverter Members

          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

          {

              string formatString = parameter.ToString();

              return String.Format("{0:" + formatString + "}", value);

          }

          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

          {

             throw new NotImplementedException("This Convert supports only OneWay binding");

          }

          #endregion

      }

  9. Delay says:

    Miguel,

    Thanks for the kind words – and brief Ruby On Rails lesson! 🙂 I think your approach to string formatting is totally reasonable and it’s something I’ve done once or twice as well. For what it’s worth, this scenario should be much easier in WPF 3.5 SP1 because they’ve added the Binding.StringFormat property (details here: http://blogs.msdn.com/vinsibal/archive/2008/05/16/new-wpf-sp1-feature-data-formatting.aspx). I’m not thinking that’ll make it into Silverlight 2, but it sure would be nice!

  10. (以下内容全部整理自博客堂Scottgu博客中文版)Silverlight技巧,诀窍,教程和链接 【原文地址】SilverlightTips,Tricks,…

  11. Delay's Blog says:

    When I was writing my video frame grabbing sample , I came up with a potentially useful technique for

  12. When we created Silverlight Charting (background reading here and here ), we tried to make things as

  13. vasche says:

    The only comment to the author – stop being a wuss and stop using "null != …" or "0 == " syntax. Or do you talk in the same way? "if seven dollars costs a beer, then anymore it’s not a happy hour". Sheesh…

  14. Delay's Blog says:

    I was recently part of an e-mail thread with Pete Brown discussing the prospects of reproducing Richard

Comments are closed.