Failed to install System.Windows.Controls.MenuItem.Icon through the setter

Hi, I am trying to install MenuItem.Icon through a style installer:

<Style x:Key="MenuItem_Delete" TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}"> <Setter Property="Header" Value="_Delete"/> <Setter Property="MenuItem.Icon"> <Setter.Value> <Image Source="Resources/Delete.png"/> </Setter.Value> </Setter> </Style> 

At runtime, I get the following exception: Unable to add contents of type "System.Windows.Controls.Image" to an object of type "System.Object". Error in object 'System.Windows.Controls.Image' in markup file 'WpfApplication1; component / application.xaml 'Line 164 Position 26.

On the other hand, this is an example in the link above:

 <MenuItem Header="New"> <MenuItem.Icon> <Image Source="data/cat.png"/> </MenuItem.Icon> </MenuItem> 

Thanks.

+6
wpf xaml icons contextmenu menuitem
source share
3 answers

I frantically searched the Internet for an answer, and I think this is a WPF bug.

I reported this to @ Microsoft Connect , please vote and confirm or share your ideas with Microsoft, if you have any.

Update
This post has helped me a lot.

+1
source share

I ran into the same problem. I found the same error in aonther thread http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/81a106dd-4d06-4506-820a-30fe96a39112 . According to their decision, you can try this. But the binding is performed only for the last item in the MenuItem collection. This is so bad!

 <Style x:Key="MenuItem_Delete" TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}"> <Style.Resources> <Image x:key="DeleteIcon" Source="Resources/Delete.png"/> </Style.Resources> <Setter Property="Header" Value="_Delete"/> <Setter Property="MenuItem.Icon" Value="{DynamicResource DeleteIcon}" /> </Style> 

Is there an update? Thanks!

+1
source share

The following code will solve this problem.

 <Style x:Key="StyleNewContext" TargetType="MenuItem"> <Style.Resources> <Image x:Key="ImageNewContext" Source="{StaticResource ImageSourceNewContext}" /> <Image x:Key="ImageNewContextDisabled" Source="{StaticResource ImageSourceNewContextDisabled}" /> </Style.Resources> <Setter Property="Icon" Value="{DynamicResource ImageNewContext}" /> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Icon" Value="{DynamicResource ImageNewContextDisabled}" /> </Trigger> </Style.Triggers> </Style> 

Regards, Peter

0
source share

All Articles