在XAML中合并WPF上下文菜单

4

在XAML中合并两个ContextMenues是否可能?

我创建了两个ContextMenues作为资源。我在一些DataTemplates中使用它们,这很好用。但是,对于某些DataTemplates,我想合并这两个ContextMenues。不幸的是,这似乎行不通。 以下是其中一个ContextMenues的代码片段,其他的定义方式相同:

<ContextMenu x:Key="CtxIEditableViewModel" DataContext="{Binding PlacementTarget,RelativeSource={RelativeSource Self}}">
    <MenuItem Header="Edit" Command="{Binding Path=DataContext.EditCommand}" CommandParameter="{Binding }">
        <MenuItem.Icon>
            <Image Source="{StaticResource IcoEdit}"  Width="16" Height="16"></Image>
        </MenuItem.Icon>
    </MenuItem>
    ...

使用其中一个上下文菜单是可以的:
<StackPanel Orientation="Horizontal" ContextMenu="{StaticResource CtxIEditableViewModel}">

但是如何合并两个呢?这种方法行不通。
<StackPanel Orientation="Horizontal">
        <ContextMenu>
            <ContextMenu.ItemsSource>
                <CompositeCollection>
                    <StaticResource ResourceKey="CtxIEditableViewModel" />
                    <StaticResource ResourceKey="CtxRootViewModel" />
                </CompositeCollection>
            </ContextMenu.ItemsSource>

这个也不起作用:

<StackPanel Orientation="Horizontal">
        <ContextMenu>
            <StaticResource ResourceKey="CtxIEditableViewModel" />
            <StaticResource ResourceKey="CtxRootViewModel" />
        </ContextMenu>

当我运行程序时,会抛出一个异常,显示上下文菜单可能不包含逻辑或可视父级。如果我只使用一个ContextMenu,则可以正常工作,我不理解异常消息的原因。
如何在XAML中合并这两个ContextMenu(或者根本不可能)?
1个回答

9

以下是使用 CompositeCollection 的一种方法:

    <Window.Resources>
    <x:Array Type="{x:Type sys:Object}" x:Key="CtxIEditableViewModel">
        <MenuItem Header="Edit1"/>
        <MenuItem Header="Edit2"/>
    </x:Array>
    <x:Array Type="{x:Type sys:Object}" x:Key="CtxRootViewModel">
        <MenuItem Header="Root1" />
        <MenuItem Header="Root2"/>
    </x:Array>              
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Border Grid.Row="0" Background="LightBlue">
        <Border.ContextMenu>
            <ContextMenu>
                <ContextMenu.ItemsSource>
                    <CompositeCollection>
                        <CollectionContainer Collection="{StaticResource CtxIEditableViewModel}" />                            
                    </CompositeCollection>
                </ContextMenu.ItemsSource>
            </ContextMenu>
        </Border.ContextMenu>
    </Border>
    <Border Grid.Row="1" Background="LightGreen">
        <Border.ContextMenu>
            <ContextMenu>
                <ContextMenu.ItemsSource>
                    <CompositeCollection>                            
                        <CollectionContainer Collection="{StaticResource CtxRootViewModel}" />
                    </CompositeCollection>
                </ContextMenu.ItemsSource>
            </ContextMenu>
        </Border.ContextMenu>
    </Border>
    <Border Grid.Row="2" Background="Khaki">
        <Border.ContextMenu>
            <ContextMenu>
                <ContextMenu.ItemsSource>
                    <CompositeCollection>                            
                        <CollectionContainer Collection="{StaticResource CtxIEditableViewModel}" />
                        <CollectionContainer Collection="{StaticResource CtxRootViewModel}" />
                    </CompositeCollection>
                </ContextMenu.ItemsSource>
            </ContextMenu>

        </Border.ContextMenu>
    </Border>
</Grid>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接