我正在使用DevExpress SnapControl
,我已设法使用自定义Data Explorer Dock Panel
替换MenuCommandHandler
中的“添加数据源”上下文菜单,因为DevExpress数据源向导需要永远加载,我们正在使用Active Query Builder
进行可视化查询构建。
我现在遇到的问题是,当我想要“删除数据源”时,我就陷入了困境。我可以像“添加数据源”一样覆盖事件,但我不知道在DataSet
中选择了哪个treeview
需要删除。
有关如何选择节点或右键单击的节点文本的任何帮助,我们将不胜感激。
因为使用MenuCommand
,CommandID
等,我有点超出我的深度。我花了一段时间才能覆盖被触发的事件本身,但最终在几个小时之后就把它想出来了。
这是我的实施
public DevExReportSnapDesignerForm()
{
InitializeComponent();
MenuCommandHandler menuCommandHandler = new CustomMenuCommandHandler(report);
menuCommandHandler.RegisterMenuCommands();
report.RemoveService(typeof(MenuCommandHandler));
report.AddService(typeof(MenuCommandHandler), menuCommandHandler);
}
public class CustomMenuCommandHandler : MenuCommandHandler
{
SnapControl snapControl;
public CustomMenuCommandHandler(SnapControl snap)
: base(snap)
{
snapControl = snap;
}
public override void UpdateCommandStatus()
{
base.UpdateCommandStatus();
DevExpress.XtraReports.Design.Commands.CommandSetItem AddDS = null;
DevExpress.XtraReports.Design.Commands.CommandSetItem RemoveDS = null;
foreach (DevExpress.XtraReports.Design.Commands.CommandSetItem item in commands)
{
if (item.CommandID == DataExplorerCommands.AddDataSource)
AddDS = item;
if (item.CommandID == DataExplorerCommands.RemoveDataSource)
RemoveDS = item;
}
if (AddDS != null)
CreateCommandEventHandler(new EventHandler(AddDS_OnClick), AddDS);
if (RemoveDS != null)
CreateCommandEventHandler(new EventHandler(RemoveDS_OnClick), RemoveDS);
}
public void CreateCommandEventHandler(EventHandler handler, MenuCommand item)
{
var mcs = snapControl.GetService(typeof(IMenuCommandService)) as IMenuCommandService;
var menuCommandID = new CommandID(item.CommandID.Guid, item.CommandID.ID);
var MenuItem = new MenuCommand(handler, menuCommandID);
mcs.RemoveCommand(item);
mcs.AddCommand(MenuItem);
}
public void AddDS_OnClick(object sender, EventArgs e)
{
//Execute Add Code
}
public void RemoveDS_OnClick(object sender, EventArgs e)
{
//Remove Code
}
}
修改
我已经在SnapFieldListTreeView
的MouseDown事件上分配了一个事件处理程序,方法是在表单构造函数中添加以下代码:
private SnapFieldListTreeView tv;
private TreeListNode dsTreeNode;
public DevExReportSnapDesignerForm()
{
InitializeComponent();
MenuCommandHandler menuCommandHandler = new CustomMenuCommandHandler(report);
menuCommandHandler.RegisterMenuCommands();
report.RemoveService(typeof(MenuCommandHandler));
report.AddService(typeof(MenuCommandHandler), menuCommandHandler);
tv = fieldListDockPanel1.Controls[0].Controls[0] as SnapFieldListTreeView;
tv.MouseClick += Tv_MouseClick;
}
private void Tv_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
dsTreeNode = tv.GetNodeAt(e.X, e.Y);
}
这样我可以检索被点击的节点。如果有更好的方法,请告诉我
答案 0 :(得分:0)
您可以直接在SnapFieldListTreeView
事件处理程序中使用RemoveDS_OnClick
实例。您只需将SnapFieldListTreeView
参数添加到自定义MenuCommandHandler
构造函数即可。当前选定的DataSet
将位于SnapFieldListTreeView.FocusedNode
媒体资源中
这是一个例子:
public DevExReportSnapDesignerForm()
{
InitializeComponent();
MenuCommandHandler menuCommandHandler = new CustomMenuCommandHandler(
report,
fieldListDockPanel1.Controls[0].Controls[0] as SnapFieldListTreeView);
menuCommandHandler.RegisterMenuCommands();
report.RemoveService(typeof(MenuCommandHandler));
report.AddService(typeof(MenuCommandHandler), menuCommandHandler);
}
public class CustomMenuCommandHandler : MenuCommandHandler
{
SnapControl snapControl;
private SnapFieldListTreeView treeView;
public CustomMenuCommandHandler(SnapControl snap, SnapFieldListTreeView tree) : base(snap)
{
snapControl = snap;
treeView = tree;
}
//Some stuff for MenuCommandHandler.
//...
public void RemoveDS_OnClick(object sender, EventArgs e)
{
var node = treeView.FocusedNode;//<= Here comes the selected DataSet's node.
MessageBox.Show(node.GetDisplayText(""));
//Remove Code
}
}