48

What is the point of this attribute? After adding it I still need to make a cast on value object.

[ValueConversion(sourceType: typeof(double), targetType: typeof(string))]
public class SpeedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var speed = (double)value;

Is it only for code readability? Because when I change a binding's path to a String in xaml, Visual Studio doesn't give a warning about incorrect type and exception is thrown only when casting, so it doesn't mean a thing even in early error catching while compiling. I also can change a cast to string and no warning is thrown despite it conflicting with this Attribute.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Lars
  • 1,699
  • 2
  • 22
  • 32

2 Answers2

35

You can potentially use the ValueConversionAttribute to determine what types are involved in the converters, and use that information usefully. Look at Piping Value Converters in WPF as an excellent example for the use of ValueConversionAttribute.

The example shows how multiple converter classes can be chained, and ValueConversion can be used to pass type info to next converter in line.

[ValueConversion( typeof( string ), typeof( ProcessingState ) )]
public class IntegerStringToProcessingStateConverter : IValueConverter
{
 object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture )
 {
  int state;
  bool numeric = Int32.TryParse( value as string, out state );
  Debug.Assert( numeric, "value should be a String which contains a number" );
  Debug.Assert( targetType.IsAssignableFrom( typeof( ProcessingState ) ), 
    "targetType should be ProcessingState" ); 

  switch( state )
  {
   case -1:
    return ProcessingState.Complete; 
   case 0:
    return ProcessingState.Pending; 
   case +1:
    return ProcessingState.Active;
  }
  return ProcessingState.Unknown;
 } 

 object IValueConverter.ConvertBack( 
    object value, Type targetType, object parameter, CultureInfo culture )
 {
  throw new NotSupportedException( "ConvertBack not supported." );
 }
}
// *************************************************************
[ValueConversion( typeof( ProcessingState ), typeof( Color ) )]
public class ProcessingStateToColorConverter : IValueConverter
{
 object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture )
 {
  Debug.Assert(value is ProcessingState, "value should be a ProcessingState");
  Debug.Assert( targetType == typeof( Color ), "targetType should be Color" );

  switch( (ProcessingState)value )
  {
   case ProcessingState.Pending:
    return Colors.Red; 
   case ProcessingState.Complete:
    return Colors.Gold; 
   case ProcessingState.Active:
    return Colors.Green;
  }
  return Colors.Transparent;
 } 

 object IValueConverter.ConvertBack( 
    object value, Type targetType, object parameter, CultureInfo culture )
 {
  throw new NotSupportedException( "ConvertBack not supported." );
 }
} 

object IValueConverter.Convert( 
    object value, Type targetType, object parameter, CultureInfo culture )
{
 object output = value; 
 for( int i = 0; i < this.Converters.Count; ++i )
 {
  IValueConverter converter = this.Converters[i];
  Type currentTargetType = this.GetTargetType( i, targetType, true );
  output = converter.Convert( output, currentTargetType, parameter, culture );

  // If the converter returns 'DoNothing' 
  // then the binding operation should terminate.
  if( output == Binding.DoNothing )
   break;
 } 
 return output;
}
//***********Usage in XAML*************
    <!-- Converts the Status attribute text to a Color -->
    <local:ValueConverterGroup x:Key="statusForegroundGroup">
          <local:IntegerStringToProcessingStateConverter  />
          <local:ProcessingStateToColorConverter />
    </local:ValueConverterGroup>
Adarsha
  • 2,267
  • 22
  • 29
16

It is just an annotation.

MSDN:

When implementing the IValueConverter interface, it is a good practice to decorate the implementation with a ValueConversionAttribute attribute to indicate to development tools the data types involved in the conversion

I do not know what the "development tools" would do with that information...

Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • It's easier to write such information in comments. Maybe it is used for auto generating documentation? – Lars Mar 09 '12 at 04:35
  • 10
    Uh-huh... Well atleast I've got a purpose in my life - write a Visual Studio add-in to make use of this attribute. – Lars Mar 09 '12 at 04:41
  • 3
    If that text originally came from that linked page, then it has since been changed. The description now merely states '**Represents an attribute that allows the author of a value converter to specify the data types involved in the implementation of the converter**'. Even less helpful now! – Sheridan Apr 12 '16 at 21:30
  • @Sheridan: Indeed. That was from the examples section of version 4 (still accessible via the `Other Versions` drop-down). – H.B. Apr 13 '16 at 13:57
  • 1
    Ahh, you are so right. It seems bizarre that they would remove the examples section from the latest page. – Sheridan Apr 13 '16 at 19:39