TreeView 中的动态上下文菜单与 MVVM

发布于 2024-08-26 12:58:41 字数 264 浏览 2 评论 0原文

我有一个显示为 TreeView 的 ViewModel 树(使用 HierarchicalDataTemplate)。每个 ViewModel 实例都有可以在其上执行的不同命令,这些命令再次公开为每个项目 ViewModel 的命令 ViewModel 列表。如何创建一个在右键单击的 TreeViewItem 处打开的单个 ContextMenu,并从基础项 ViewModel 的命令 ViewModel 列表中填充其命令?一切都以一种体面的 MVVM 方式进行......

I have a tree of ViewModels displayed as a TreeView (using HierarchicalDataTemplate). Each ViewModel instance has different commands that can be executed on it wich again are exposed as a list of command ViewModels for each item ViewModel. How can I create a single ContextMenu that opens at the TreeViewItem that was rightclicked and that populates its commands from the underlying item ViewModel's command ViewModels list? All in a decent MVVM fashion ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临走之时 2024-09-02 12:58:41

我想我理解你的问题。我认为你可以像这样构建你的 ViewModel:

interface ICommandViewModel : ICommand
{
  string Name {get;}
}

interface INodeViewModel
{
  IEnumerable<ICommandViewModel> CommandList {get;}
}

public class NodeViewModel : INodeViewModel
{
  public NodeViewModel()
  {
    //Init commandList
    //Populate commandList here(you could also do lazy loading)
  }

  public NodeViewModel(IEnumerable<ICommandViewModel> commands)
  {
    CommandList = commands;
  }

  public IEnumerable<ICommandViewModel> CommandList {get;private set;}
}

然后在 xaml 中

<TreeViewItem>
  <TreeViewItem.ContextMenu Items={Binding CommandList}>
    <ContextMenu.ItemTemplate>
      <DataTemplate>
        <MenuItem Header="{Binding Name}" Command="{Binding}"/>
      </DataTemplate>
    </ContextMenu.ItemTemplate>
  </TreeViewItem.ContextMenu>
</TreeViewItem>

我对分层数据模板没有太多经验,但你可以从上面得到要点。您也可以使用 中的样式执行上述操作,但我面前没有 xaml 编辑器来为您提供正确的语法。

希望有帮助

I think I understand your question. I think that you could structure your ViewModels like this:

interface ICommandViewModel : ICommand
{
  string Name {get;}
}

interface INodeViewModel
{
  IEnumerable<ICommandViewModel> CommandList {get;}
}

public class NodeViewModel : INodeViewModel
{
  public NodeViewModel()
  {
    //Init commandList
    //Populate commandList here(you could also do lazy loading)
  }

  public NodeViewModel(IEnumerable<ICommandViewModel> commands)
  {
    CommandList = commands;
  }

  public IEnumerable<ICommandViewModel> CommandList {get;private set;}
}

and then in xaml

<TreeViewItem>
  <TreeViewItem.ContextMenu Items={Binding CommandList}>
    <ContextMenu.ItemTemplate>
      <DataTemplate>
        <MenuItem Header="{Binding Name}" Command="{Binding}"/>
      </DataTemplate>
    </ContextMenu.ItemTemplate>
  </TreeViewItem.ContextMenu>
</TreeViewItem>

I don't have much experience with hierarchical datatemplate but you get the gist from the above. You could also do the above with a style in the but I don't have a xaml editor in front of me to give you right syntax.

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文