刷新IValueConverter的枚举会引发异常,试图将System.String强制转换为IValueConverter?

时间:2012-09-17 20:07:23

标签: wpf exception data-binding enums ivalueconverter

我正在尝试将枚举转换为画笔,以便枚举变量可用于控制某些控件的颜色

我的枚举(不太相关):

public enum Colors {
        Red, Blue,
    }

这是我的转换器:

[ValueConversion(typeof(Colors), typeof(Brush))]
public class EnumToBrushConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return (Brushes.Red);
    }

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

这里我试图用它来根据枚举值更改标签的颜色(Color是Colors类型的公共属性):

<Grid>
    <Grid.Resources>
        <conv:EnumToBrushConverter x:Key="EnumToBrushConverter" />
    </Grid.Resources>
    <Label Content="fixed" Foreground="{Binding Path=Color, Converter=EnumToBrushConverter}" />
</Grid>

构建窗口时,我遇到以下异常:

System.Windows.Markup.XamlParseException occurred
  Message='Set property 'System.Windows.Data.Binding.Converter' threw an exception.' Line number '9' and line position '11'.
  Source=PresentationFramework
  LineNumber=9
  LinePosition=11
  StackTrace:
       at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at TestCollectionChangingListbox.MainWindow.InitializeComponent() in c:\Users\stevez\Documents\Visual Studio 2010\Projects\TestCollectionChangingListbox\TestCollectionChangingListbox\MainWindow.xaml:line 1
       at TestCollectionChangingListbox.MainWindow..ctor() in C:\Users\stevez\Documents\Visual Studio 2010\Projects\TestCollectionChangingListbox\TestCollectionChangingListbox\MainWindow.xaml.cs:line 29
  InnerException: System.InvalidCastException
       Message=Unable to cast object of type 'System.String' to type 'System.Windows.Data.IValueConverter'.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.Baml2006.WpfSharedBamlSchemaContext.<Create_BamlProperty_Binding_Converter>b__14c(Object target, Object value)
            at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
       InnerException: 

2 个答案:

答案 0 :(得分:6)

您需要将转换器作为静态资源访问;试试这个:

<Label Content="fixed" Foreground="{Binding Path=Color, Converter={StaticResource EnumToBrushConverter}}" />

答案 1 :(得分:5)

您需要在相关控件的资源部分创建该转换器的实例(如果您想在应用程序的多个位置使用转换器,则需要在app.xaml文件中创建):

<UserControl.Resources>
    <local:EnumToBrushConverter x:Key="EnumToBrushConverter " />
</UserControl.Resources>

然后将该转换器引用为静态资源:

<Label Content="fixed" Foreground="{Binding Path=Color, Converter={StaticResource EnumToBrushConverter}" />