为了将枚举转换为图标,我使用了一个值转换器,如下所示:
public class IconConverter : IValueConverter
{
public ResourceDictionary Items { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string key = Enum.GetName(value.GetType(), value);
return Items[key];
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}我在XAML中使用它,如下所示:
<UserControl.Resources>
<local:IconConverter x:Key="IconConverter">
<ResourceDictionary Source="/Leister.WPFControls;component/ButtonStyles.xaml" />
</local:IconConverter>
</UserControl.Resources>当我启动应用程序时,一切工作正常,转换器转换一个值的名称,并通过它的键从ResourceDictionary中获取图标。但在我的设计器中,Visual Studio2010总是抱怨:
The object of type System.Windows.ResourceDictionary" can not be cast to type "Microsoft.Expression.DesignModel.DocumentModel.DocumentNode".
at Microsoft.Expression.DesignModel.Core.InstanceBuilderOperations.SetValue(Object target, IProperty propertyKey, Object value)
at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.ModifyValue(IInstanceBuilderContext context, ViewNode target, IProperty propertyKey, Object value, PropertyModification modification)
at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.UpdateProperty(IInstanceBuilderContext context, ViewNode viewNode, IProperty propertyKey, DocumentNode valueNode)这真是太棒了!有什么想法吗?有没有更简单的解决方案来将枚举转换成XAML-Icon Resources?
发布于 2011-07-20 04:43:06
我知道现在已经很晚了,但也许ContentPropertyAttribute可以帮助设计师:
[ContentProperty("Items")]
public class IconConverter : IValueConverter
{
public ResourceDictionary Items { get; set; }http://msdn.microsoft.com/en-us/library/system.windows.markup.contentpropertyattribute.aspx
https://stackoverflow.com/questions/4681107
复制相似问题