无法通过设置器设置 System.Windows.Controls.MenuItem.Icon
您好,我正在尝试使用 MenuItem.Icon 通过样式设置器设置:
<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>
我在运行时收到以下异常: 无法将“System.Windows.Controls.Image”类型的内容添加到“System.Object”类型的对象。标记文件“WpfApplication1;component/application.xaml”第 164 行第 26 行中的对象“System.Windows.Controls.Image”出错。
另一方面,这是上面链接中的示例:
<MenuItem Header="New">
<MenuItem.Icon>
<Image Source="data/cat.png"/>
</MenuItem.Icon>
</MenuItem>
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题。
我在另一个线程 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/81a106dd-4d06-4506-820a-30fe96a39112。
根据他们的解决方案,你可以尝试这个。但绑定仅对 MenuItem 集合中的最后一个元素执行。太糟糕了!
有更新吗?谢谢!
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!
Is there any update? Thanks!
我拼命在网上寻找答案,我认为这是一个 WPF 错误。
我已向 Microsoft Connect 报告了该问题,请投票并验证或如果您有想法,请与 Microsoft 分享您的想法。
更新
这篇文章对我帮助很大。
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.
Update
This post helped me a lot.
下一个代码将解决这个问题。
问候,
彼得
The next code will solve this issue.
Regards,
Peter