代码之家  ›  专栏  ›  技术社区  ›  Michael Baldry

wpf treeviewitem dyanmic上下文菜单

  •  0
  • Michael Baldry  · 技术社区  · 15 年前

    我在WPF中的数据绑定TreeView遇到一些问题,基本上我希望上下文菜单数据绑定到我的TreeView项目ViewModel上的IEnumerable属性,这是我在每个TreeView项目中要做的:

    <Setter Property="ContextMenu">
    <Setter.Value>
        <ContextMenu ItemsSource="{Binding ContextMenu}" />
    </Setter.Value>
    

    但在加载XAML时,它给出了一个异常,即它不能在System.Object或这些行上设置ContextMenu。

    有人能解释一下吗?

    谢谢

    1 回复  |  直到 15 年前
        1
  •  1
  •   viky    15 年前

    在样式中声明为资源,然后将其作为staticresource赋值

    <Style>
      <Style.Resources>
        <ContextMenu x:Key="contextmenustyle" ItemsSource="{Binding ContextMenu}" />
      </Style.Resources>
      <Setter Property="ContextMenu" Value="{StaticResource contextmenustyle}">
    </Style>