【问题标题】:WPF Combobox selection behaviorWPF 组合框选择行为
【发布时间】:2013-04-11 20:42:49
【问题描述】:

WPF ComboBox 控件允许通过两种方式使用鼠标更改选择。

  • 用鼠标向下/向上单击,弹出窗口出现,然后单击要选择的项目。

  • 单击并按住。弹出窗口出现,您将鼠标悬停在要选择的项目上并释放鼠标按钮。选择 MouseUp 事件时鼠标悬停的项目。

有没有办法消除behavior #2?即让他们执行 2 次完整的向下/向上单击以更改选择?

【问题讨论】:

  • 我用鼠标向下、向上、向下预览和向上预览。预览后总是会触发 no help selectionchanged。

标签: c# wpf combobox mouseevent


【解决方案1】:

它可能并不漂亮,但结合一些事件似乎可以满足您的需求:

private bool _comboMouseDown = false;
private bool _comboSelectionDisabled = false;

private void ComboBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    _comboMouseDown = true;
}

private void ComboBox_DropDownOpened(object sender, EventArgs e)
{
    if (_comboMouseDown)
    {
        //Don't enable selection until the user releases the mouse button:
        _comboSelectionDisabled = true;
    }
}

private void ComboBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
    if (_comboSelectionDisabled)
    {
        //Stop the accompanying "MouseUp" event (which would select an item) from firing:
        e.Handled = true;

        _comboSelectionDisabled = false;
    }

    _comboMouseDown = false;
}

1) 仍然正常工作

2) 单击并按住仍会打开弹出窗口,但您需要松开并再次单击以选择项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多