简体   繁体   English

使用HierarchicalDataTemplate将命令绑定到ContextMenu项

[英]Bind Command to ContextMenu items with HierarchicalDataTemplate

I have a Button which shows a ContextMenu look like 我有一个按钮,显示ContextMenu看起来像

按钮菜单

Here the XAML: 这里是XAML:

  <Window.Resources>

    <local:BindingProxy x:Key="proxy" Data="{Binding}" />

    <Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}">
      <Setter Property="Command" Value="{Binding Data.OnSelected, Source={StaticResource proxy}}" />
    </Style>


    <Button Name="ButtonMenu_Export" 
          Click="ButtonMenu_Export_Click"
          Visibility="{Binding ButtonExportEnabled, 
                        Converter={StaticResource VisibilityConverter}}">
        <StackPanel Orientation="Vertical">
            <Image Source="...." />
            <TextBlock Width="70" TextAlignment="Center" Text="Export" />
        </StackPanel>
        <Button.ContextMenu>
            <ContextMenu ItemsSource="{Binding ExportMenuItems}">
                <ContextMenu.ItemTemplate>
                  <HierarchicalDataTemplate ItemContainerStyle="{StaticResource MenuItemStyle}">
                    <ContentPresenter Content="{Binding Text}" RecognizesAccessKey="True" />
                    <HierarchicalDataTemplate.ItemsSource>
                      <Binding Path="SubItems" />
                    </HierarchicalDataTemplate.ItemsSource>
                  </HierarchicalDataTemplate>
                </ContextMenu.ItemTemplate>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>

The menu is created at runtime using this List (as in this article ) 菜单是在运行时使用此List创建的(如本文所述

public System.Collections.Generic.List<MenuItem> ExportMenuItems
{
  get { return _menuService.GetParentMenuItems(); }
}

Now, what I cannot do is bind the items to the OnSelected command of MenuItem class. 现在,我不能做的就是将项目绑定到MenuItem类的OnSelected命令。

The class which defines the menu is: 定义菜单的类为:

  public class MenuItem
  {
    private string name;
    private string text;
    private int menuId;
    private ICommand onSelected;
    private MenuItem parent;
    private ObservableCollection<MenuItem> subItems;

    public MenuItem(string name, string text, int MenuId)
    {
      this.menuId = MenuId;
      this.name = name;
      this.text = text;
      this.subItems = new ObservableCollection<MenuItem>();
    }

    public string Name { get { return this.name; } }

    public string Text { get { return this.text; } }

    public MenuItem Parent { get { return this.parent; } set { this.parent = value; } }

    public ICommand OnSelected
    {
      get
      {
        if (this.onSelected == null)
          this.onSelected = new MenuCommand(this.ItemSelected, this.ItemCanBeSelected, menuId);

        return this.onSelected;
      }
    }

    public ObservableCollection<MenuItem> SubItems
    {
      get { return this.subItems; }
    }

  }

I created a proxy class as in this article to made DataContext visible to HierarchicalDataTemplate content but maybe I misunderstood something: 我按照本文中的方法创建了一个代理类,以使HierarchicalDataTemplate内容可以看到DataContext,但也许我误解了:

  public class BindingProxy : Freezable
  {
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
      return new BindingProxy();
    }

    #endregion

    public object Data
    {
      get { return (object)GetValue(DataProperty); }
      set { SetValue(DataProperty, value); }
    }

    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
  }

Where I'm wrong? 我哪里错了?

更改命令绑定

Command="{Binding Data.OnSelectedd,Source={StaticResource proxy}, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM