Solved

Add an icon to WPF contextmenu from resources in code behind

Posted on 2011-09-28
2
1,136 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
2 Comments
 
LVL 4

Accepted Solution

by:
theHollow earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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