【问题标题】:Event when combobox is selected选择组合框时的事件
【发布时间】:2014-09-10 11:15:24
【问题描述】:

我的问题是如何在 C# (WPF) 的 ComboBox 中选择 ComboBoxItem 时执行操作?

this post 中,他们处理 DropDownClosed 事件,但他们不处理键盘选择

所以我解释一下我的情况:

ComboBoxItems 的“Selected”事件或 ComboBox 的“SelectionChanged”事件仅在用户选择不同的 ComboBoxItem 时执行该操作,但我希望即使用户选择的 ComboBoxItem 相同,该操作也能执行ComboBoxItem 已经被选中。

我尝试使用“PreviewMouseLeftButtonDown”,但如果用户使用键盘选择或按住鼠标然后选择,它不起作用。

在我的情况下,当我选择一个项目时,我必须打开一个窗口:

private void cmiCCSelect_Selected(object sender, RoutedEventArgs e)
{
    cCEntityWindow.ShowDialog();
}

但是如果用户关闭这个窗口并重新选择同一个项目,它就不起作用了。我必须选择另一个,然后重新选择相同的事件“已选择”才能执行。

谁能帮帮我?

【问题讨论】:

  • 看来我对Enter 不感兴趣。很抱歉

标签: c# wpf combobox


【解决方案1】:

我终于找到了答案:

您需要像这样处理 SelectionChanged 事件和 DropDownClosed:

在 XAML 中:

<ComboBox Name="cmbSelect" SelectionChanged="ComboBox_SelectionChanged" DropDownClosed="ComboBox_DropDownClosed">
    <ComboBoxItem>1</ComboBoxItem>
    <ComboBoxItem>2</ComboBoxItem>
    <ComboBoxItem>3</ComboBoxItem>
</ComboBox>

在 C# 中:

private bool handle = true;
private void ComboBox_DropDownClosed(object sender, EventArgs e) {
    if(handle)Handle();
    handle = true;
}

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    ComboBox cmb = sender as ComboBox;
    handle = !cmb.IsDropDownOpen;
    Handle();
}

private void Handle() {
    switch (cmbSelect.SelectedItem.ToString().Split(new string[] { ": " }, StringSplitOptions.None).Last())
    { 
        case "1":
            //Handle for the first combobox
            break;
        case "2":
            //Handle for the second combobox
            break;
        case "3":
            //Handle for the third combobox
            break;
    }
}

【讨论】:

    猜你喜欢
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多