[英]How to add icon from project properties in XAML
Hi I was wondering if there is possibility to get access to project properties resources form xaml. 嗨,我想知道是否有可能通过xaml访问项目属性资源。 I need to create a menuItem icon, so normally I would do this that way
我需要创建一个menuItem图标,所以通常我会这样做
itemCopy = new MenuItem
{
Icon = System.Drawing.Icon.FromHandle(NameSpace.Properties.Resources.iconName.GetHicon())
};
However I would like to create the same icon directly from XAML without using code behind. 但是,我想直接从XAML创建相同的图标,而无需使用后面的代码。 Is this possible
这可能吗
You need to set up the respective namespace and do a static reference, eg 您需要设置各自的名称空间并进行静态引用,例如
<Window ...
xmlns:prop="clr-namespace:Test.Properties"
Title="{Binding Source={x:Static prop:Resources.WinTitle}}">
If your reference does not provide an object of the right type or a method call is necessary you could use a ValueConverter . 如果您的引用没有提供正确类型的对象,或者必须进行方法调用,则可以使用ValueConverter 。
Icons in resx resources are of type System.Drawing.Icon
, but WPF needs an ImageSource
. resx资源中的图标
type System.Drawing.Icon
,但是WPF需要一个ImageSource
。 Just change the build action of the icon to "Resource" (not "Embedded Resource"), and refer to it directly in XAML: 只需将图标的生成操作更改为“资源”(而不是“嵌入式资源”),然后在XAML中直接引用它即可:
<MenuItem Icon="Resources/yourIcon.ico"...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.