使用HierarchicalDataTemplate将命令绑定到ContextMenu项

时间:2016-01-07 10:51:57

标签: c# wpf xaml

我有一个显示ContextMenu外观的按钮

Button Menu

这里是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>

使用此列表(如this article

在运行时创建菜单
public System.Collections.Generic.List<MenuItem> ExportMenuItems
{
  get { return _menuService.GetParentMenuItems(); }
}

现在,我不能做的是将项目绑定到MenuItem类的OnSelected命令。

定义菜单的类是:

  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; }
    }

  }

我在this article中创建了一个代理类,使得DataContext对HierarchicalDataTemplate内容可见,但也许我误解了一些东西:

  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));
  }

我哪里错了?

1 个答案:

答案 0 :(得分:0)

更改命令绑定

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