2
考虑一个示例WPF应用程序,它具有带滚动的图像查看器。 用户可以通过按箭头键以及通过鼠标滚动来滚动图像。如何在C#WPF中相互排斥两个事件(互斥事件)?
如何相互排除这两个事件Keydown和MouseWheel。 ?
在MouseWheel(滚动)事件期间,按键事件不应起作用,反之亦然。
我已经尝试使用标志,但它不工作。
我试过的示例代码。用你的符号
internal bool KeyPress; // by default false
internal bool IsMouseScrollActive; // by default false
private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (!KeyPress)
{
IsMouseScrollActive = true;
// event handlers here
IsMouseScrollActive = false;
}
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Left:
case Key.Down:
case Key.Right:
case Key.Up:
if (!IsMouseScrollActive)
{
KeyPress = true;
// event handlers here
}
break;
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Left:
case Key.Down:
case Key.Right:
case Key.Up:
if (!IsMouseScrollActive)
{
KeyPress = false;
// event handlers here
}
break;
}
}
你说的'在鼠标滚轮(滚动)事件,键按下事件意味着不应该工作,反之亦然。“?你是否希望事件'Grid_MouseWheel'和'Window_KeyDown'不被同时触发或其他东西? – CarbineCoder
是的。当发生事件处理程序时,不应该同时触发事件处理程序。 – raj
事件处理函数的默认行为满足您的条件 – CarbineCoder