首页 > 解决方案 > 如何跟踪 ItemsControl 的哪个元素导致 ContexMenu 打开?

问题描述

在我的图书编辑器应用程序中,我需要按层次顺序显示主题和子主题,所以我做了这样一个标记:

<StackPanel>
      <ScrollViewer>
        <Grid ColumnDefinitions="auto, *" RowDefinitions="auto">
          <ItemsControl Grid.Column="0" Grid.Row="0" Items="{Binding Book.Themes}">
            <ItemsControl.ItemTemplate>
              <DataTemplate>
                <StackPanel>
                  <Button Content="{Binding Name}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.UpdateSubthemesVisibilityCommand}" CommandParameter="{Binding Name}"/>
                  <ItemsControl Items="{Binding Subthemes}" IsVisible="{Binding AreSubthemesVisible}">
                    <ItemsControl.ItemTemplate>
                      <DataTemplate>
                        <Button Content="{Binding Name}"/>
                      </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ContextMenu>
                      <ContextMenu>
                        <MenuItem Header="Rename" />
                        <MenuItem Header="Delete"/>
                      </ContextMenu>
                    </ItemsControl.ContextMenu>
                  </ItemsControl>
                </StackPanel>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ContextMenu>
              <ContextMenu>
                <MenuItem Header="Rename" Command="{Binding RenameThemeCommand}" CommandParameter="...How to bind it to button which cause menu to open"/>
                <MenuItem Header="Delete"/>
              </ContextMenu>
            </ItemsControl.ContextMenu>
          </ItemsControl>
        </Grid>
      </ScrollViewer>
    </StackPanel>

也许标记不是很清楚,但关键是我有外部 ItemsControl 和内部的。外部项目具有结构:按钮(主题名称)+ 内部 ItemsControl,它也将子主题的名称显示为按钮。这是屏幕截图:
在此处输入图像描述

每个 ItemsControl 都有自己的 ContextMenu,但我无法正确绑定它们。我希望“重命名”MenuItems 绑定到 Button.Content 的 Button 右键单击​​导致菜单打开。我应该怎么做?
我正在使用 MVVM Avalonia,但我希望在 WPF 中它的工作方式相同,以便我可以将 WPF 主题标签添加到这个问题中。

标签: wpfavalonia

解决方案


您不需要将 ContextMenu 添加到 ItemsControl。它需要被添加到 ItemsControl 中。
这是在 ItemContainerStyle 中完成的。
在这种情况下,在 ContextMenu DataContext 中将包含调用 ContextMenu 的元素。

例子:

    <Window.Resources>
        <spec:StringCollection x:Key="strings">
            <sys:String>First</sys:String>
            <sys:String>Second</sys:String>
            <sys:String>Third</sys:String>
        </spec:StringCollection>
        <Style x:Key="ItemStyle" TargetType="ContentPresenter">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <TextBlock Text="{Binding}"/>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <ItemsControl ItemsSource="{StaticResource strings}"
                  ItemContainerStyle="{StaticResource ItemStyle}"/>

推荐阅读