?
Solved

Add an icon to WPF contextmenu from resources in code behind

Posted on 2011-09-28
2
Medium Priority
?
1,196 Views
Last Modified: 2013-11-12
How can I add an icon to a WPF contextmenu from the application resources in code behind?

The resource can be either an icon or png, whichever is easiest.
0
Comment
Question by:CatsSolutions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
theHollow earned 1500 total points
ID: 36719739
Hi
You may approach this by adding a property in your CodeBehind or ViewModel.
I recommend using a ViewModel (using the MVVM pattern) because it may (in my opinion) reduce code and improve scalability.
But you can also make this in your VodeBehind.

First of all, you will need to implement the "INotifyPropertyChanged".

Then add some properties:
public String MenuItemHeader
{
    get { return (String)GetValue(MenuItemHeaderProperty); }
    set
    {
        SetValue(MenuItemHeaderProperty, value);
        NotifyPropertyChanged("MenuItemHeader");
    }
}

public String MenuItemIcon
{
    get { return (String)GetValue(MenuItemIconProperty); }
    set
    {
        SetValue(MenuItemIconProperty, value);
        NotifyPropertyChanged("MenuItemIcon");
    }
}

Open in new window


And some dependency properties for the properties:
public static readonly DependencyProperty MenuItemHeaderProperty = DependencyProperty.Register("MenuItemHeader", typeof(String), typeof(MyClassName), new PropertyMetadata(null));
public static readonly DependencyProperty MenuItemIconProperty = DependencyProperty.Register("MenuItemIcon", typeof(String), typeof(MyClassName), new PropertyMetadata(null));

Open in new window


The ContextMenu on your control may look like this:
<SomeControl.ContextMenu>
    <ContextMenu>
        <MenuItem Header="{Binding MenuItemHeader}"
                  Icon="{Binding MenuItemImage}"/>
    </ContextMenu>
</SomeControl.ContextMenu>

Open in new window


Then you can set this code to set the ContextMenu text and icon:
MenuItemHeader = "This is the contextmenu text";
MenuItemIcon = "/MyWpfApplication;component/Images/save.png";

Open in new window


But there are many ways.
You amy also place "x:Name="SomeControl"" on the controls and add it "the old way" :-)
That's up to you to decide.

Hope it helps :-)
0
 

Author Closing Comment

by:CatsSolutions
ID: 36813849
Thanks
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question