小编Ale*_*lex的帖子

wpf - 上下文菜单 - 在动态资源中重用静态资源

我有一个用户控件,资源如下:

<UserControl.Resources>

    <Image x:Key="IconPrinter" Source="/PrintViewerWPF;component/Resources/Images/printer.png" />

    <MenuItem x:Key="PrintDoc" 
              Header="Print" 
              Click="PrintDoc_OnClick"
              Icon="{StaticResource IconPrinter}" />

    <ContextMenu x:Key="MergedContextMenu">
        <StaticResource ResourceKey="PrintDoc"/>
    </ContextMenu>

    <ContextMenu x:Key="SingleContextMenu">
        <StaticResource ResourceKey="PrintDoc"/>
    </ContextMenu>

    <DataTemplate x:Key="mergedDocDisplayGrid">
        <StackPanel StackPanel.ContextMenu="{DynamicResource MergedContextMenu}" />
    </DataTemplate>

    <DataTemplate x:Key="singleDocDisplayGrid">
        <StackPanel StackPanel.ContextMenu="{DynamicResource SingleContextMenu}" />
    </DataTemplate>

</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

所述DataTemplates的一个的DevExpress内使用FlowLayoutControl

在我的场景中,我有两个项目FlowLayoutControl.一个使用模板mergedDocDisplayGrid,另一个使用singleDocDisplayGrid

数据在模板中正确显示,并且已成功实现其他功能,如鼠标活动和拖放

在任一项上显示上下文菜单都有效,但如果我随后尝试再次在另一项上显示上下文菜单,则会出现以下异常:

Message - Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.

Inner - Element already has a logical parent. It must be detached from the old parent before it …
Run Code Online (Sandbox Code Playgroud)

c# wpf contextmenu dynamicresource staticresource

0
推荐指数
1
解决办法
2704
查看次数

标签 统计

c# ×1

contextmenu ×1

dynamicresource ×1

staticresource ×1

wpf ×1