gpt4 book ai didi

wpf - 在上下文菜单中分组

转载 作者:行者123 更新时间:2023-12-05 01:11:35 25 4
gpt4 key购买 nike

如果我有一个命令列表,其中每个命令都有一个组号,我该如何在上下文菜单中显示这些命令?

下面是我试过的代码,但我似乎无法显示子菜单项。

namespace groupTest
{
public class GroupedCommand
{
public string Name { get; set; }
public ICommand Command { get; set; }
public int Group { get; set; }
}

public partial class Window1 : Window
{
public List<GroupedCommand> Commands
{
get;
private set;
}

public Window1()
{
Commands = new List<GroupedCommand>();
Commands.Add(new GroupedCommand() { Name = "A", Group = 0 });
Commands.Add(new GroupedCommand() { Name = "B", Group = 0 });
Commands.Add(new GroupedCommand() { Name = "C", Group = 1 });
Commands.Add(new GroupedCommand() { Name = "D", Group = 1 });

var defView = (CollectionView)CollectionViewSource.GetDefaultView(Commands);
defView.GroupDescriptions.Add(new PropertyGroupDescription("Group"));

DataContext = this;
InitializeComponent();
}
}
}

<Window x:Class="groupTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Window.ContextMenu>
<ContextMenu ItemsSource="{Binding Commands}">
<ContextMenu.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<MenuItem Header="{Binding}">
<ItemsPresenter />
</MenuItem>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ContextMenu.GroupStyle>
</ContextMenu>
</Window.ContextMenu>
</Window>

最佳答案

我已设法通过更改 GroupItem 的控件模板和附加行为来实现此功能。

我已经替换了下面的 ControlTemplate

<ControlTemplate TargetType="{x:Type GroupItem}">
<MenuItem Header="{Binding}">
<ItemsPresenter />
</MenuItem>
</ControlTemplate>

<ControlTemplate TargetType="{x:Type GroupItem}">
<MenuItem Header="{Binding}" ItemsSource="{Binding Items}" Style="{StaticResource GroupMenuItemStyle}" />
</ControlTemplate>

然后添加了一个附加行为以在鼠标进入/离开 MenuItem 时显示/隐藏子菜单项

public static class GroupMenuBehavior
{
public static readonly DependencyProperty DummyProperty =
DependencyProperty.RegisterAttached("Dummy", typeof (bool), typeof (GroupMenuBehavior), new PropertyMetadata(default(bool), PropertyChangedCallback));

private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var mi = dependencyObject as MenuItem;
if (mi == null) return;
if (mi.IsLoaded)
{
AddHandlers(mi);
}
else
{
mi.Loaded += MiOnLoaded;
}
}

private static void MiOnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
var mi = (MenuItem) sender;
mi.Loaded -= MiOnLoaded;
AddHandlers(mi);
}

static void AddHandlers(MenuItem mi)
{
mi.MouseEnter += MiOnMouseEnter;
mi.MouseLeave += MiOnMouseLeave;
}

private static void MiOnMouseLeave(object sender, MouseEventArgs mouseEventArgs)
{
var mi = (MenuItem)sender;
if (mi.IsSubmenuOpen)
mi.IsSubmenuOpen = false;
}

private static void MiOnMouseEnter(object sender, MouseEventArgs mouseEventArgs)
{
var mi = (MenuItem) sender;
if (mi.Items != null && mi.Items.Count != 0)
mi.IsSubmenuOpen = true;
}

public static void SetDummy(MenuItem menuItem, bool value)
{
menuItem.SetValue(DummyProperty, value);
}

public static bool GetDummy(MenuItem menuItem)
{
return (bool) menuItem.GetValue(DummyProperty);
}
}

并且,MenuItem 的样式附加上述行为:

<Style TargetType="{x:Type MenuItem}" x:Key="GroupMenuItemStyle">
<Setter Property="local:GroupMenuBehavior.Dummy" Value="True" />
</Style>

我知道,这是一个丑陋的破解/修复。但是,无法通过其他方式让它工作。

关于wpf - 在上下文菜单中分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16753856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com