绑定模式下的转换器:
1、创建转换器
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Data; 6 7 namespace Mytest 8 { 9 /// <summary> 10 /// 将01 02与布尔类型相互转换 11 /// </summary> 12 public class RdbConvertToStr : IValueConverter 13 { 14 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 15 { 16 if (value != null && !string.IsNullOrEmpty(value.ToString())) 17 { 18 if (value.ToString() == parameter.ToString()) 19 { 20 return true; 21 } 22 } 23 return false; 24 } 25 26 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 27 { 28 if (value != null) 29 { 30 if ((bool)value) 31 { 32 return parameter.ToString(); 33 } 34 } 35 return ""; 36 } 37 } 38 39 /// <summary> 40 /// 将01 02 转为是否可见 41 /// </summary> 42 public class RdbConvertToVisiable : IValueConverter 43 { 44 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 45 { 46 if (value != null && !string.IsNullOrEmpty(value.ToString())) 47 { 48 if (value.ToString() == "01") 49 return "Visiable"; 50 else 51 return "Collapsed"; 52 } 53 return "Collapsed"; 54 } 55 56 57 58 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 59 { 60 if (value != null) 61 { 62 if (value.ToString() == "Visiable") 63 { 64 return "01"; 65 } 66 else 67 return "02"; 68 } 69 return ""; 70 } 71 } 72 }
2、在使用页面添加转换器引用
xmlns:convert="clr-namespace:Mytest.Converter"
3、对使用的转换器进行命名
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Mytest;component/Index.xaml"/>
</ResourceDictionary.MergedDictionaries>
<convert:RdbConvertToVisiable x:Key="rdbConvert"/>
</ResourceDictionary>
</UserControl.Resources>
4、绑定转换器
Visibility="{Binding MyEntity.TestEntity.G000000B,Converter={StaticResource rdbConvert}}"