WPF ScrollViewer嵌套的DataGrid、ListBox等控件的鼠标滚动事件无效,子控件拦截父控件滚动效果解决办法
注册子控件的PreviewMouseWheel,然后在滚动的时候设置控件的 IsHitTestVisible = false,因为这个参数设为false之后子控件里面的TextBox,CheckBox会失效,所以在停止滚动后要把参数设回来。代码如下
private void girdOrder_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { RetryUtil.TimeOutAction(200, () => { this.Dispatcher.Invoke(() => { this.IsHitTestVisible = false; }); }, () => { this.Dispatcher.Invoke(() => { this.IsHitTestVisible = true; }); }); }
下面是TimeOutAction的代码片
/// <summary> /// /// </summary> /// <param name="TS">超时等待时长ms</param> /// <param name="OnAction">执行任务</param> /// <param name="TimeAction">超时处理任务</param> public static void TimeOutAction(int TS, Action OnAction, Action TimeAction) { CancellationTokenSource cts = new CancellationTokenSource(TS); cts.Token.Register(() => {//等待设置时间后执行 TimeAction(); }); Task.Factory.StartNew(() => {//立即执行 while (!cts.Token.IsCancellationRequested) { OnAction(); Task.Delay(TS).Wait(); } }, cts.Token); }