上下文菜单在某些情况下被剪裁
我目前正在制作一个从左键单击而不是右键单击打开的上下文菜单,为此,我通过处理这样的 ContextMenuOpening 事件来禁止右键单击
private void PinBorder_ContextMenuOpening(object sender, System.Windows.Controls.ContextMenuEventArgs e)
{
e.Handled = true;
}
,并且我自己根据对 MouseButtonLeftDown 事件的反应打开上下文菜单:
private void PinBorder_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
PinBorder.ContextMenu.PlacementTarget = PinBorder;
PinBorder.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Center;
PinBorder.ContextMenu.HorizontalOffset = 0;
PinBorder.ContextMenu.VerticalOffset = 0;
PinBorder.ContextMenu.IsOpen = true;
e.Handled = true;
}
这里的问题是,当第一次打开 ContextMenu 时,一切顺利,但是如果我将一个项目添加到绑定到上下文菜单的可观察集合中并尝试重新打开它,上下文菜单将被裁剪为之前的大小(如果我尝试使用向上/向下键移动上下文菜单选择我可以猜测已经创建了一个条目,但我看不到它,因为它被剪裁了)。
我尝试删除点击抑制的东西,在这种情况下一切都很顺利。
我在 .net Framework 3.5 中读到过这样的问题,但我的目标是 4.0。
有人有解决办法吗?
I am currently making a context menu which open from left button click instead of right button click and to do that I inhibit the right click by handling the ContextMenuOpening event like this
private void PinBorder_ContextMenuOpening(object sender, System.Windows.Controls.ContextMenuEventArgs e)
{
e.Handled = true;
}
and I open the context menu by myself on reaction to the MouseButtonLeftDown event like this :
private void PinBorder_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
PinBorder.ContextMenu.PlacementTarget = PinBorder;
PinBorder.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Center;
PinBorder.ContextMenu.HorizontalOffset = 0;
PinBorder.ContextMenu.VerticalOffset = 0;
PinBorder.ContextMenu.IsOpen = true;
e.Handled = true;
}
the problem here is when the ContextMenu is opened the first time everything goes well but if I add an item to the observable collection bound to the context menu and try to reopen it, the context menu is clipped to its previous size (if I try to move the context menu selection with up/down key I can guess that an entry has been created but I can't see it because it is clipped).
I tried to remove the click inhibition stuffs and every thing goes well in that case.
I read about such an issue in .net framework 3.5 but i am targeting 4.0.
Does anyone has a solution ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以以这种方式模拟事件:
然而,使用 UIAutomation 并不简单(我的示例实际上不起作用,但应该为您指明正确的方向)。
您还可以尝试将类似的内容添加到您已有的内容中(在函数之前):
有用的 msdn 参考。
You can simulate an event in this fashion:
Using the UIAutomation isn't simple however (and my example doesn't actually work but should point you in the right direction).
You could also try adding something like this to what you already have (before it in your function):
Useful msdn reference.
如果最终找到了一种解决方法来获得我想要的东西,即使该解决方案必须分配比所需更多的内存。
每次必须打开上下文菜单时,我都会重新创建整个上下文菜单。
If finally found a workaround to get what I want even if this solution must allocate more memory than necessary.
I recreate the whole contextmenu each time the context menu must open.
我不确定你做错了什么。以下内容符合我的要求:
ViewModel:
I'm not sure what you're doing wrong. The following works as desired for me:
ViewModel: