views:

1167

answers:

4

Hi I am trying to have a MenuItem.Icon set thru a style setter:

<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>

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

In the other hand, this is the example in the above link:

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

Thanks.

A: 

To be able to style something like that, the source has to be freezable. Images are not by default.

You can get around this by wrapping it in a static resource. Static resources are implicitly frozen and don't have this limitation:

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

This ought to work.

Anderson Imes
No. It doesn't work: Cannot convert the value in attribute 'Style' to object of type 'System.Windows.Style'. 'System.Windows.Controls.Image' is not a valid value for 'Setter.Value'; values derived from Visual or ContentElement are not supported. Error at object 'mnuEdit' in markup file 'WpfApplication1;component/pages/contactspage.xaml'.
Shimmy
Ah... you likely have to wrap this in a template. I'll update the post after I try it myself tomorrow to avoid any more errors. Use what I just edited at your own risk ( could still be slightly wrong ).
Anderson Imes
It compiles, and in the Menuitem I see instead of the image a string representing the full name of the DataTemplate type.
Shimmy
A: 

I have run into the same issue. I found the same error on aonther thread http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/81a106dd-4d06-4506-820a-30fe96a39112. According to their solution, you may try this one. But binding executes only for last element in MenuItem collection. It's 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 any update? Thanks!

Chuanyu
Doesn't seem to work.
Shimmy
A: 

Hi,

The next code will solve this issue.

<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

Peter Farkas
That's not what I asked but it doesn't work anyway.
Shimmy
+2  A: 

I was desperatly searching the web for an answer and I think this is a WPF bug.

I reported it @ Microsoft Connect, please vote and validate or share your ideas with Microsoft if you have some.

Shimmy