1

我有一个 WPF 按钮,其内容有一堆控件,包括一个 CheckBox。

目前单击 CheckBox 会触发“Checked”事件,这很好。

我不喜欢的是 Double-Click 也会触发“Checked”事件。

目前,双击会触发 CheckBox 的“Checked”事件和 Button 的“DoubleClicked”事件。

我想要的是 CheckBox 上的双击应该只触发 Button 的事件,而不是 CheckBox 的事件。CheckBox 应该只在单击时被选中。

有没有办法做到这一点?

谢谢。

4

1 回答 1

1

What you trying to do might not make for the best user experience because you're going to have to implement a delay prior to processing a user's attempt to check the box.

Why?

Well, if you double click the checkbox, you want nothing to happen with the checkbox, you just want to pass control up to the button. That's totally understandable.

But what happens if you simply click the checkbox? At the moment you release the mouse button, you are going to expect the check mark to appear. But the system can't yet know that you don't plan to click again. So, it has to wait through the DoubleClickTime to determine that you in fact never double-clicked.

You have to weigh which is worse: delay when checking the checkbox vs. flicker when double-clicking the checkbox.

If you decide to go with the delay, you'll need to implement it yourself. Here's a basic procedure that should work:

  1. Create a handler for CheckBox.PreviewMouseDown. Within this handler, disable the CheckBox and start a DispatcherTimer with an interval of DoubleClickTime.
  2. Meanwhile, create a handler for Button.MouseDoubleClick that sets a field, _hasDoubleClicked, to true.
  3. When the timer goes off, enable the checkbox, and if _hasDoubleClicked is false, toggle the IsChecked of the checkbox. Also, set _hasDoubleClick = false.

As you can see this is not pretty. You might want to ask yourself if there is another way to achieve your goal with a different design. Do you need to use double-click in the first place?

于 2013-08-10T01:47:13.743 回答