ValueConversionAttribute类的呢? [英] The point of ValueConversionAttribute class?

查看:470
本文介绍了ValueConversionAttribute类的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么属性点? 。添加它后,我还需要做出价值对象上投

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;



难道只为代码的可读性?
,因为当我更改为XAML字符串绑定的路径时,Visual Studio不给浇注时,警告有关不正确的类型和异常抛出只,所以这并不意味着,即使在早期的错误而醒目的事编译。
我还可以更改强制转换为字符串,尽管它与这个属性相冲突没有警告被抛出。

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.

推荐答案

您有可能使用 ValueConversionAttribute 来确定所涉及的转换器是什么类型,并有效地使用这些信息。
看在WPF 管道值转换作为一个很好的例子使用 ValueConversionAttribute

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.

的例子显示了转换器类如何将多个可以链接,并ValueConversion可以用来通过类型信息来下的在线转换器。

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>

这篇关于ValueConversionAttribute类的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆