I have found a strange behavior for RadContextMenu.
I have a class that inherits from RadMenuItem. It contains some properties I need for specific scenario, but none of them are related to the problem, so I have excluded them from the posted code.
public
class
MyContextMenuItem : RadMenuItem
{
// code excluded for brevity
}
Then, there's a constructor for that class that looks like this:
public
MyContextMenuItem(String text, String imageName =
null
,
object
userState =
null
)
{
if
(imageName !=
null
)
{
DataTemplate template =
Application.Current.Resources[
"cm_"
+ imageName]
as
DataTemplate;
if
(template !=
null
)
IconTemplate = template;
}
Header = text;
// rest of the code not relevant or related to Icons
// ...
}
There you can see that I have set the IconTemplate for the specific menu item.
I have tested and ensured that the Application.Current.Resources really returns the DataTemplate from the dictionary:
<
ResourceDictionary
xmlns:x
=
"http://schemas.microsoft.com/winfx/2006/xaml"
>
<
DataTemplate
x:Key
=
"cm_edit"
>
<
Image
Source
=
"/MyProject;component/Images/edit.png"
/>
</
DataTemplate
>
<
DataTemplate
x:Key
=
"cm_exit"
>
<
Image
Source
=
"/MyProject;component/Images/exit.png"
/>
</
DataTemplate
>
</
ResourceDictionary
>
Context menu with items created this way DOES NOT show the icons.
I am opening the context menu on button click:
contextMenu.SetControl(btnTest,
"Click"
);
Then I thought maybe that the paths were incorrect, so I've taken my images and put them into that button to see if it can show them:
<
Button
x:Name
=
"btnTest"
Width
=
"100"
Height
=
"20"
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
Image
Source
=
"/MyProject;component/Images/edit.png"
/>
<
Image
Source
=
"/MyProject;component/Images/exit.png"
/>
</
StackPanel
>
</
Button
>
Then I ran the application and the button was correctly displaying the images.
But here comes the strange part. Now that I've set the same icons in that button, now even the ContextMenu shows the images.
How is that possible? It seems as if the images have to be already loaded in some way for them to be able to show in Context menu.
Note that I've tried even setting the images directly, by not using the IconTemplate, but setting it like this:
String path =
"/MyProject;component/Images/edit.png"
;
Uri uri =
new
Uri(path, UriKind.Relative);
BitmapImage img =
new
BitmapImage(uri);
contextMenuItem.Icon =
new
Image() { Source = img };
The behaviour is exactly the same, when that same icons are already used (by setting it via XAML) the context menu would show them, otherwise it wouldn't. Also, it doesn't matter that they are in the button related to the ContextMenu. The icons can be defined anywhere on the page (but thru XAML) and then ContextMenu would be able to show them.
I don't understand why would loading the images in code-behind be any different than in XAML, and for some reasons we have to programatically set them. Is that possible without this behavior occuring? How to deal with this problem?