WPF绑定菜单项与有界子菜单

时间:2017-03-25 19:42:08

标签: c# wpf mvvm menuitem

我正在尝试创建一个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)
    })
});

目前的结果: enter image description here

1 个答案:

答案 0 :(得分:0)

不要在MenuItem中添加ItemTemplate元素。 MenuItem中的每个项都隐式生成ItemsSource个元素。您应该将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>