Solved

Add an icon to WPF contextmenu from resources in code behind

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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