ValueConversionAttribute类的重点是什么?

时间:2012-03-09 04:16:57

标签: c# wpf ivalueconverter

这个属性有什么意义?添加它之后,我仍然需要对值对象进行强制转换。

[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;

仅用于代码可读性吗? 因为当我在xaml中将绑定的路径更改为String时,Visual Studio不会发出关于错误类型的警告,并且仅在转换时抛出异常,因此即使在编译时早期错误捕获也不意味着什么。 我也可以将强制转换为字符串,尽管它与此属性冲突,但不会抛出任何警告。

2 个答案:

答案 0 :(得分:27)

您可以使用ValueConversionAttribute来确定转换器中涉及的类型,并有用地使用该信息。 请将Piping Value Converters in WPF视为使用ValueConversionAttribute的绝佳示例。

该示例显示了如何链接多个转换器类,并且可以使用ValueConversion将类型信息传递给下一个转换器。

[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>

答案 1 :(得分:14)

这只是一个注释。

MSDN

  

在实现IValueConverter接口时,最好使用 ValueConversionAttribute 属性修饰实现,以向开发工具指示转换中涉及的数据类型

我不知道“开发工具”会对这些信息做些什么......