Solved

Add an icon to WPF contextmenu from resources in code behind

Posted on 2011-09-28
2
1,119 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now