样式设置器中的上下文菜单
有谁知道为什么这会产生 XamlParseException“无法将'System.Windows.Controls.ContextMenu'类型的内容添加到'System.Object'类型的对象”:
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas ClipToBounds="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Remove" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
并且以下工作正常?
<ItemsControl>
<ItemsControl.Resources>
<ContextMenu x:Key="NodeContextMenu">
<MenuItem Header="Remove" />
</ContextMenu>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas ClipToBounds="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="ContextMenu" Value="{StaticResource NodeContextMenu}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
Does anyone know why this would produce an XamlParseException "Cannot add content of type 'System.Windows.Controls.ContextMenu' to an object of type 'System.Object'":
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas ClipToBounds="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Remove" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
And the following works just fine?
<ItemsControl>
<ItemsControl.Resources>
<ContextMenu x:Key="NodeContextMenu">
<MenuItem Header="Remove" />
</ContextMenu>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas ClipToBounds="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="ContextMenu" Value="{StaticResource NodeContextMenu}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来像是一个已修复的错误 - 当针对 .NET 3.5 时,相关代码会生成 XAML 解析异常,但当针对 .NET 4.0 时,它对我来说工作得很好。如果您点击上面评论中提供的链接,您将发现一个连接问题,其他人的评论表明该问题已在 .NET 4.0 中消失。
Looks like a bug that has since been fixed - the code in question generates a XAML parse exception when targeting .NET 3.5, but works fine for me when targeting .NET 4.0. If you follow the link provided in the comments above, you will find a connect issue with comments from others indicating the problem has gone away with .NET 4.0.