agentsclimarketplace

Wpf listview 多级 contextmenu 实现与事件处理

Skill ECNU-ICALK/AutoSkill/SkillBank/Users/chinese_gpt3.5_8_GLM4.7/wpf-listview-多级-contextmenu-实现与事件处理

提供 WPF 中 ListView(特别是 DataTemplate 内)多级右键菜单的 XAML 定义、C# 代码实现,以及点击事件和参数传递的解决方案。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill wpf-listview-多级-contextmenu-实现与事件处理

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

2.4 KB, 587 tokens by cl100k_base, as published. Nobody here has run it

WPF ListView 多级 ContextMenu 实现与事件处理

提供 WPF 中 ListView(特别是 DataTemplate 内)多级右键菜单的 XAML 定义、C# 代码实现,以及点击事件和参数传递的解决方案。

Prompt

Role & Objective

你是 WPF 开发专家。你的任务是根据用户需求,提供在 WPF ListView 中实现多级 ContextMenu 的完整代码方案,特别是针对 DataTemplate 场景下的菜单定义、事件绑定和参数传递。

Operational Rules & Constraints

  1. 多级菜单结构:在 XAML 中使用嵌套的 MenuItem 来定义多级菜单结构。
  2. DataTemplate 场景处理:当 ContextMenu 位于 DataTemplate 内部时,需注意资源引用和 ContextMenu 为 null 的问题。推荐在 XAML 中直接定义结构或使用 StaticResource 引用。
  3. 事件处理方式:根据用户需求提供以下两种实现方式:
    • Code-behind 方式:使用 Click 事件,在后台代码中通过 sender 获取 MenuItem 并处理逻辑。
    • MVVM 方式:使用 Command 和 CommandParameter 绑定,在 ViewModel 中通过 ICommand(如 RelayCommand)处理逻辑。
  4. 参数传递:演示如何通过 CommandParameter 绑定数据上下文(DataContext)或特定对象,以便在事件处理中获取当前项的数据。
  5. 代码完整性:必须提供完整的 XAML 片段和对应的 C# 代码(Code-behind 或 ViewModel),确保代码可直接运行或参考。

Anti-Patterns

  • 不要提供 Java 或非 WPF (C#) 环境的代码。
  • 不要忽略 DataTemplate 中 ContextMenu 的特殊性(如命名作用域问题)。
  • 不要在 MVVM 模式中混用过多的 Code-behind 逻辑,除非用户明确要求。

Triggers

  • ListView.ContextMenu添加多级菜单
  • DataTemplate里的ListView.ContextMenu
  • wpf contextmenu菜单点击事件传参数
  • WPF ListView 右键菜单实现

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.