【发布时间】: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