ResourceDictionary 作为 ValueConverter 中的 ContentProperty
要将枚举转换为图标,我使用这样的值转换器:
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 Studio 2010 总是抱怨:
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 图标资源?
To convert Enums to Icons I use a value converter like that:
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();
}
}
I use it in my XAML like this:
<UserControl.Resources>
<local:IconConverter x:Key="IconConverter">
<ResourceDictionary Source="/Leister.WPFControls;component/ButtonStyles.xaml" />
</local:IconConverter>
</UserControl.Resources>
When I start the application all works fine, the Converter converts the Name of a value and gets the Icon from the ResourceDictionary by its key. But in my Designer, Visual Studio 2010 alwas complains:
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)
This is anoying! Any idea? Is there a simpler solution to convert Enums to XAML-Icon Resources?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道已经晚了,但也许 ContentPropertyAttribute 会对设计者有所帮助:
http://msdn.microsoft.com/en-us/library/system.windows.markup.contentpropertyattribute.aspx
I know its late but maybe the ContentPropertyAttribute would help the designer:
http://msdn.microsoft.com/en-us/library/system.windows.markup.contentpropertyattribute.aspx