How do I set MenuItem's Icon using ItemContainerStyle

data-binding, icons, menuitem, mvvm, wpf

Solution

WPF works with `ImageSource`s, not `System.Drawing` classes. You'll need to bind to an `ImageSource`. You could use a converter to convert your `Bitmap` to an `ImageSource`, or you could ditch the resources and do things differently.

Problem

I'm following the example here of binding a `MenuItem` to a data object. ``` <Menu Grid.Row="0" KeyboardNavigation.TabNavigation="Cycle" ItemsSource="{Binding Path=MenuCommands}"> <Menu.ItemContainerStyle> <Style> <Setter Property="MenuItem.Header" Value="{Binding Path=DisplayName}"/> <Setter Property="MenuItem.ItemsSource" Value="{Binding Path=Commands}"/> <Setter Property="MenuItem.Command" Value="{Binding Path=Command}"/> <Setter Property="MenuItem.Icon" Value="{Binding Path=Icon}"/> </Style> </Menu.ItemContainerStyle> </Menu> ``` It all works swimmingly except the `MenuItem`'s icon shows up as the string `System.Drawing.Bitmap`. The bitmap in question is returned by the data object from a compiled resource. ``` internal static System.Drawing.Bitmap folder_page { get { object obj = ResourceManager.GetObject("folder_page", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } ``` What am I doing wrong?

Original source

Related problems