开发者

How to add icon from project properties in XAML

开发者 https://www.devze.com 2023-02-25 10:27 出处:网络
Hi I was wondering if there is possibility to get access to project properties resources form xaml. I need to create a menuItem icon, so normally I would do this that way

Hi I was wondering if there is possibility to get access to project properties resources form xaml. I need to create a menuItem icon, so normally I would do this that way

itemCopy = new MenuItem
                           {

                               Icon = System.Drawing.Icon.FromHandle(NameSpace.Properties.Resources.iconName.Get开发者_C百科Hicon())
                           };

However I would like to create the same icon directly from XAML without using code behind. Is this possible


You need to set up the respective namespace and do a static reference, e.g.

<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.


Icons in resx resources are of type System.Drawing.Icon, but WPF needs an ImageSource. Just change the build action of the icon to "Resource" (not "Embedded Resource"), and refer to it directly in XAML:

<MenuItem Icon="Resources/yourIcon.ico"...
0

精彩评论

暂无评论...
验证码 换一张
取 消