WPF Menu Icon Changing Problem
I'm having problems with WPF menu and the Icons. The thing i'd like to do is something like that:
Menu1 - NOICON op1
NOICON opt2
ICON opt3
Menu2 - NOICON opt4
ICON opt5
I have a Menu like that:
<Menu>
<MenuItem Header="Engine" Click="MenuItem_Click_12" >
<MenuItem Header="Use SCCP Standard" Click="MenuItem_Click_11" x:Name="SCCP"/>
<MenuItem Header="Use ITS" Click="MenuItem_Click_10" x:Name="ITS"/>
<MenuItem Header="Use ODE" Click="MenuItem_Click_13" x:Name="ODE"/>
</MenuItem>
<MenuItem Header="Constraint Store" >
<MenuItem Header="muParser" Click="MenuItem_Click_15" x:Name="muParser"/>
<MenuItem Header="Flee" Click="MenuItem_Click_14" x:Name="Flee"/>
</MenuItem>
</Menu>
The code Behind the click is:
private void MenuItem_Click_14(object sender, RoutedEventArgs e)
{
CS_TYPE = "Flee";
Flee.Icon = CHECK;
muParser.Icon = NOCHECK;
}
For EVERY click event... When i click (for sample) on Flee, it gets the right image and muParser also... But every icon in the other menu suddendly disappeared!!!
开发者_JAVA技巧What's wrong with my example? Why icons disappeared from 2 different menu?
Thank you very much!!!
You need to make sure that you are creating a new Image for each MenuItem. A single Image cannot be used in multiple places.
This is a common mistake, and is probably what you are running into. An Image is a visual, just like a Button. If you set the MenuItem.Icon of two MenuItem's to the same Image, then that Image would have to appear in the visual tree in two locations (which isn't allowed).
You can create a new Image from the old one, by copying the Image.Source value.
精彩评论