【问题标题】:XAML designer error "Unable to cast object of type 'System.Windows.StaticResourceExtension' to type 'System.Windows.Data.IValueConverter'."XAML 设计器错误“无法将'System.Windows.StaticResourceExtension' 类型的对象转换为'System.Windows.Data.IValueConverter'。”
【发布时间】:2015-10-06 04:44:54
【问题描述】:

我正在使用开源 XAML 标记扩展从声明的 enum 类型填充 <ComboBox>,但在设计器中我收到这些错误(蓝色波浪线下划线文本):

无法将“System.Windows.StaticResourceExtension”类型的对象转换为“System.Windows.Data.IValueConverter”类型。

这是导致错误的 XAML(错误在 ItemsSource 属性和值上):

<ComboBox
    ItemsSource="{local:Enumerate {x:Type p:FoobarEnum}, {StaticResource e2s}}"
    SelectedItem="{Binding Foobar, Converter={StaticResource e2s}}"
/>

e2s 资源在 ResourceDictionary 中声明:

<v:EnumToStringConverter x:Key="e2s" />

还有Enumerate 标记扩展is from this GitHub project

[ValueConversion( typeof( Enum ), typeof( String ) )]
public class EnumToStringConverter : IValueConverter
{
    public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
    {
        ...
    }
    public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
    {
        ...
    }
}

[MarkupExtensionReturnType( typeof( IEnumerable<Object> ) )]
public sealed class EnumerateExtension : MarkupExtension
{
    public EnumerateExtension()
    {
    }

    public EnumerateExtension(Type type, Object converter)
    {
        ...
        // converter needs to be Object otherwise WPF complains with other errors
    }
}

奇怪的是,当程序运行并且EnumerateExtension 正确地将枚举的成员(也已本地化)加载到 ComboBox 中时,这实际上是有效的。

【问题讨论】:

    标签: wpf xaml markup-extensions


    【解决方案1】:

    将您的 ItemsSource 属性值更改为:

    ItemsSource="{local:Enumerate Type={x:Type p:FoobarEnum}, Convertor={StaticResource e2s}}"
    

    这将工作 110%。

    问题是 XAML 解析器无法确定哪个用于 Type,哪个用于 Convertor。排序和显式提及在 XAML 中很重要。

    【讨论】:

    • 当我这样做时,我得到这个设计器并构建错误:“在解析标记扩展时遇到类型'MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension'的未知属性'转换器'。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2023-04-03
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多