具有有界子菜单的 WPF 绑定菜单项

WPF Binding menu item with bounded Submenu

我正在尝试创建一个 2 层子菜单,但由于某些原因,第二层无法呈现。

XAML:

<MenuItem Header="Minor Graph" ItemsSource="{Binding GraphMenuItems}">
    <MenuItem.ItemTemplate>
        <DataTemplate>
            <MenuItem Header="{Binding Header}" Command="{Binding MenuItemCommand}" ItemsSource="{Binding SubItems}">
                <MenuItem.ItemTemplate>
                    <DataTemplate>
                        <MenuItem Header="{Binding Header}" Command="{Binding MenuItemCommand}" />
                    </DataTemplate>
                </MenuItem.ItemTemplate>
            </MenuItem>
        </DataTemplate>
    </MenuItem.ItemTemplate>
</MenuItem>

我的视图模型:

public class MenuViewModel<T> : BindableBase
{
    private string _header;
    private ObservableCollection<MenuViewModel<T>> _subItems;
    private Action<T> OnClickItem;
    private T ValueItem;

    public MenuViewModel(string header,
        T item,
        Action<T> onClickItem)
        : this(header, item, new List<MenuViewModel<T>>(), onClickItem)
    {

    }
    public MenuViewModel(string header,
        List<MenuViewModel<T>> subItems)
        : this(header, default(T), subItems, obj => { })
    {
    }
    private MenuViewModel(string header,
        T item,
        List<MenuViewModel<T>> subItems,
        Action<T> onClickItem)
    {
        Header = header;
        SubItems = new ObservableCollection<MenuViewModel<T>>(subItems);
        OnClickItem = onClickItem;
        ValueItem = item;
    }

    public string Header
    {
        get { return _header; }
        set { SetProperty(ref _header, value); }
    }

    public ObservableCollection<MenuViewModel<T>> SubItems
    {
        get { return _subItems; }
        set { SetProperty(ref _subItems, value); }
    }

    private ICommand _menuItemCommand;
    public ICommand MenuItemCommand
    {
        get
        {
            if (_menuItemCommand == null)
            {
                _menuItemCommand = new DelegateCommand(() =>
                {
                    OnClickItem(ValueItem);
                });
            }
            return _menuItemCommand;
        }
    }
}

GraphMenuItems 的值属性:

GraphMenuItems = new ObservableCollection<MenuViewModel<string>>(new List<MenuViewModel<string>>()
{
    new MenuViewModel<string>("LAeq reading", "LAeg", OnMinorGraphSettingsChanged),
    new MenuViewModel<string>("Octave band 1/1", new List<MenuViewModel<string>>()
    {
        new MenuViewModel<string>("16 Hz", "1-1-Hz16",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("31.5 Hz", "1-1-Hz31_5",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("16 Hz", "1-1-Hz16",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("63 Hz", "1-1-Hz63",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("125 Hz", "1-1-Hz125",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("250 Hz", "1-1-Hz250",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("500 Hz", "1-1-Hz500",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("1000 Hz", "1-1-Hz1000",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("2000 Hz", "1-1-Hz2000",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("4000 Hz", "1-1-Hz4000",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("8000 Hz", "1-1-Hz8000",  OnMinorGraphSettingsChanged),
        new MenuViewModel<string>("16000 Hz", "1-1-Hz16000",  OnMinorGraphSettingsChanged)
    })
});

当前结果:

不要在 ItemTemplate 中添加 MenuItem 元素。 ItemsSource 中的每个项目都隐式生成了一个 MenuItem 元素。您应该将 ItemTemplate 设置为 HierarchicalDataTemplate

试试这个:

<Menu>
    <Menu.Resources>
        <Style TargetType="MenuItem">
            <Setter Property="Command" Value="{Binding MenuItemCommand}" />
        </Style>
    </Menu.Resources>
    <MenuItem Header="Minor Graph" ItemsSource="{Binding GraphMenuItems}">
        <MenuItem.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
                <TextBlock Text="{Binding Header}" />
            </HierarchicalDataTemplate>
        </MenuItem.ItemTemplate>
    </MenuItem>
</Menu>