gpt4 book ai didi

wpf - 样式 setter 中的ContextMenu

转载 作者:行者123 更新时间:2023-12-04 16:13:12 29 4
gpt4 key购买 nike

有谁知道为什么这会产生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>

最佳答案

看起来像一个已修复的错误-有问题的代码在面向.NET 3.5时会生成XAML解析异常,但在面向.NET 4.0时对我来说效果很好。如果您单击上面的注释中提供的链接,则会发现连接问题以及来自其他人的注释,表明该问题已随.NET 4.0消失。

关于wpf - 样式 setter 中的ContextMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3280121/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com