样式设置器中的上下文菜单

发布于 2024-09-10 07:15:38 字数 1561 浏览 2 评论 0原文

有谁知道为什么这会产生 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2024-09-17 07:15:38

看起来像是一个已修复的错误 - 当针对 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文