2015-12-20 45 views
-1

我嘗試將簡單string並添加標記string例如:WPF:得到無法強制轉換對象異常,當嘗試轉換

Value is: bla bla 

到:Value is <Span Foreground="Red">bla bla</Span>

所以我想用MultiValueConverter並添加簡單的轉換器(至今沒有任何實現):

public class StatusConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

Window.Resources:

<my:StatusConverter x:Key="StatusConverterToColor"/> 

用法:

<TextBlock Text="{Binding Status, Converter={StaticResource StatusConverterToColor}}" /> 

但得到這個錯誤:

{"Unable to cast object of type 'MyApplication.classes.StatusConverter' to type 'System.Windows.Data.IValueConverter'."}

什麼,我做錯了什麼?

回答

1

更改IMultiValueConverterIValueConverter。聲明應該是

public class StatusConverter : IValueConverter 
{ ... } 
+0

而與IValueConverter我將能夠改變我的字符串,並添加此標記? –

+0

是的,只要在'Convert'方法中提供真正的實現,而不是拋出'NotImplementedException' – Eldar

相關問題