【问题标题】:Context sub menu with items source and additional items带有项目源和附加项目的上下文子菜单
【发布时间】:2011-08-02 16:48:19
【问题描述】:

我在 wpf 中有一个上下文菜单。菜单中的一项具有从标题菜单项的 ItemsSource 填充的子菜单。此子菜单是可以发送到应用程序其他部分的命令列表。该列表基本上是一个限制为 10 项的 mru 列表。我想在 10 个项目的列表下方添加一个分隔符,然后添加一个“更多”选项,以便用户可以看到可用命令的整个列表。我似乎无法弄清楚如何添加这些额外的项目。我可以从父菜单项的 ItemsSource 动态填充列表,但我似乎无法弄清楚如何将其他项目添加到子菜单的底部。我不想将它们放在项目源中,并且“更多”项目需要有自己的命令。

<MenuItem x:Name="ExecuteCommandMenuItem" Height="22" Style="{StaticResource RightClickMenuItemStyle}"
                          ItemsSource="{Binding Path=PanelCommands}">
                    <MenuItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Panel Command" HorizontalAlignment="Left" Width="100"/>
                        </StackPanel>
                    </MenuItem.Header>

                    <MenuItem.ItemContainerStyle>
                        <Style TargetType="MenuItem" BasedOn="{StaticResource RightClickMenuItemStyle}">
                            <Setter Property="MenuItem.Header" Value="{Binding}" />
                            <Setter Property="MenuItem.Command" Value="CommonCommands:CommandRepository.ExecutePanelCommand" />
                            <Setter Property="MenuItem.CommandParameter">
                                <Setter.Value>
                                    <MultiBinding Converter="{CommonConverter:PanelCommandArgsConverter}">
                                        <MultiBinding.Bindings>
                                            <Binding Path="DataContext" RelativeSource="{RelativeSource FindAncestor, 
                                                             AncestorType={x:Type ContextMenu}}"/>
                                            <Binding Path="Command" />
                                        </MultiBinding.Bindings>
                                    </MultiBinding>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </MenuItem.ItemContainerStyle>
                </MenuItem>

谢谢。

【问题讨论】:

标签: c# .net wpf


【解决方案1】:
<DataGrid x:Class="UICCNET.BaseControls.UserControls.BaseDataGrid"
         Tag="{Binding RelativeSource={RelativeSource Self}, Path=Columns}">
<DataGrid.ContextMenu>
    <ContextMenu Tag="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Tag}">
        <MenuItem Header="Колонки">
        <MenuItem  >              
            <MenuItem.Template>
                <ControlTemplate>
                        <ListBox  Name="MyComboBox"  ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}},Path=Tag}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding Path=Visibility, Mode=TwoWay, Converter={StaticResource BooleanToVisibilityConverter1}}" Content="{Binding Path=Header}"></CheckBox>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </ControlTemplate>
            </MenuItem.Template>
                 </MenuItem>
        </MenuItem>
        <MenuItem Header="Друк"></MenuItem>
    </ContextMenu>
</DataGrid.ContextMenu>

【讨论】:

    【解决方案2】:

    我不认为你可以,因为它绑定到一个源。因此,要么将它们添加到源中,要么使用模板选择器并在那里执行你的逻辑。定义一个普通模板,然后定义一个“更多”模板。

    或者,您可以进行一些控件嵌套,例如

    <menu>
    <stackpanel>
    <Menu Items>
    
    </menu Items>
    <break />
    <Button>More</button>
    </stackpanel>
    </menu>
    

    对不起,这只是我的想法。您可以发布您的 XAML 吗?

    【讨论】:

    • 谢谢。我添加了我的 xaml。我会试试你的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多