Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to put this in data template code:

HTML
<menuitem.header>
    <Image Source="/Assembly;component/Resources/Images/Img.png"
           Margin="-3,-1,0,0"
           Stretch="None" />
</menuitem.header>


I have this code so far:

C#
var headerMenuIcon = new FrameworkElementFactory(typeof(Image));

headerMenuIcon.SetValue(Image.SourceProperty, new BitmapImage(new Uri("Resources/Images/Img.png", UriKind.Relative)));

var headerMenuItem = new FrameworkElementFactory(typeof(MenuItem));

headerMenuItem.SetValue(MenuItem.HeaderProperty, headerMenuIcon);


However, it only displays a string representation of the icon framework element factory as follows: "System.FrameworkElementFactory" inside the menu header.

How can I put an image in a menu item header in a data template from code?
Posted
Comments
Mark Salsbery 9-Nov-11 15:12pm    
I've never used FrameworkElementFactory because it's been deprecated for some time, but how can you set a concrete instance (object) of a BitmapImage to a template?

FWIW - "The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900