转换器:提供将自定义逻辑应用于绑定的方法 。
页面显示的值也许不是直观的值,只是如1,2,3...这些数字代码, 转换器可将数据从一种类型更改为另一种类型,例如将数字代码转为具体案例值。
转换器代码:
public class CusTomConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int)
return "这是" + value;
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
ViewModel代码:
private int _tName = 1;
public int TName
{
get { return _tName; }
set
{
_tName = value;
this.RaisePropertyChanged("TName");
}
}
xaml文件:转换器写在ViewModel中,所以要引入xmlns:viewModel="clr-namespace:WriteTXT.ViewModels"。
页面显示: