WPF菜单图标更改问题
我在使用 WPF 菜单和图标时遇到问题。 我想做的事情是这样的:
Menu1 - NOICON op1
NOICON opt2
ICON opt3
Menu2 - NOICON opt4
ICON opt5
我有一个这样的菜单:
<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>
点击背后的代码是:
private void MenuItem_Click_14(object sender, RoutedEventArgs e)
{
CS_TYPE = "Flee";
Flee.Icon = CHECK;
muParser.Icon = NOCHECK;
}
对于每个点击事件... 当我在 Flee 上单击(作为示例)时,它会获得正确的图像,并且 muParser 也会... 但是其他菜单中的所有图标突然消失了!
我的例子有什么问题吗?为什么图标从 2 个不同的菜单中消失?
非常感谢!!!
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!!!
What's wrong with my example? Why icons disappeared from 2 different menu?
Thank you very much!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确保为每个菜单项创建一个新图像。单个图像不能在多个地方使用。
这是一个常见的错误,也可能是您遇到的错误。图像是一种视觉效果,就像按钮一样。如果将两个 MenuItem 的 MenuItem.Icon 设置为同一图像,则该图像必须出现在可视化树中的两个位置(这是不允许的)。
您可以通过复制 Image.Source 值从旧图像创建新图像。
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.