首页 > 代码库 > 2、IValueConverter应用

2、IValueConverter应用

1、C#代码如下:

   public class logotoimgConverter:IValueConverter   {       //将logo转换为URI       public object Convert(object value, Type targetType, object parameter, string language)       {           logo c = (logo)value;           switch (c)           {               case logo.Baidu:                   return @"/Assets/Baidu.png";                                  case logo.Google:                   return @"/Assets/Google.png";//   /这个才是/正斜杠               default:                   return null;           }       }       public object ConvertBack(object value, Type targetType, object parameter, string language)       {           throw new NotImplementedException();       }   }

2、在XAML中,

resource中有

 <local:logotoimgConverter x:Key="logC"/>

而在相对应要转换值的绑定的控件加入

 <Image Width="100" Height="100" Source="{Binding Path=logo, Converter={StaticResource logC}}"/>

 

2、IValueConverter应用