在我的图书编辑器应用程序中,我需要按层次顺序显示主题和子主题,所以我做了这样一个标记:
<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 主题标签添加到这个问题中。