1.建类,必须继承IValueConverter接口,在命名空间System.Windows.Data下
class BoolToContentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool temp = bool.Parse(value.ToString());
if (temp)
return "暂 停";
else
return "开 始";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
2.在资源文件xaml里加入 转换器,converters为命名空间定义 比如xmlns:converters="clr-namespace:Converters"
<converters:BoolToContentConverter x:Key="BoolToContentConverter"/>
3.使用转换器
<Button Content="{Binding Path=isDownloading, Converter={StaticResource BoolToContentConverter}}" > </Button>