如何在wpf .net中设置菜单ID [英] how to set id for menu in wpf .net

查看:106
本文介绍了如何在wpf .net中设置菜单ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我可以从数据库中获取动态菜单名称,但是我不知道为菜单设置ID,因为我想基于菜单ID获取详细信息.

Hi,
i can get dynamic menu name form the database, but i don''t know to set id for menu because i want to get details based on menu id.

推荐答案

这与ID无关.您的问题是在运行时数据库数据不足时创建菜单.代替ID,将接口类型ICommand的命令分别添加到每个菜单项.请参见System.Windows.Controls.MenuItem http://msdn.microsoft.com/en-us /library/system.windows.controls.menuitem.aspx [ ^ ].
请注意以下属性:CommandCommandParameterCommandTarget.

阅读有关使用命令的信息: http://msdn.microsoft.com/en-us/library/ms752308.aspx [ ^ ].

当您正在寻找自动生成菜单时,建议您采用最简单的方法.为所有菜单项设置仅一个常用命令!所有项目都是平等的.要使它们不同,请使用属性System.Windows.Controls.MenuItem.Tag向每个项目添加一些数据,这些数据随后将用于命令调用.我不知道标记所需的数据结构,这取决于您的数据模型,但是您可以设计处理每个单独菜单项中的单击所需的任何数据结构.将此标签的值(类型转换为其实际运行类型标签)传递给命令参数.在处理命令时使用此数据项.

—SA
This is not about IDs. You problem is creating Menu during run-time out of the database data. Instead of IDs, there is a command of the interface type ICommand added to each menu item individually. See System.Windows.Controls.MenuItem, http://msdn.microsoft.com/en-us/library/system.windows.controls.menuitem.aspx[^].
Pay attention for the properties: Command, CommandParameter, CommandTarget.

Read about using commanding: http://msdn.microsoft.com/en-us/library/ms752308.aspx[^].

As you''re looking for auto-generating of the menu, I would suggest you take the simplest approach. Set up only one common command for all menu items! All items are equal. To make them different use the property System.Windows.Controls.MenuItem.Tag to add some data to each item which later will be used for command invocation. I don''t know what data structure you will need for the tag, it depends on your data model, but you can devise any data structure you need to process the click in each individual menu item. Pass the value of this tag (type-casted to its real run-type tag) to the command parameter. Use this data item in handling the command.

—SA


这篇关于如何在wpf .net中设置菜单ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆