事件根本没有被解雇

时间:2013-03-20 13:31:19

标签: c# .net wpf windows events

我有一个ComboBox,有很多ComboBoxItems。当用户使用鼠标悬停在ComboBoxItem上时,我想触发一个事件。这是我到目前为止尝试的代码,但是事件没有触发 - 即当我放置一个断点时,事件没有输入。

private void cmbValue_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ComboBox cmb = sender as ComboBox;
            cmb.Items.Clear();

            //Iterates through all virtual tables
            foreach (TableContainer table in listOpenUnjoinedTables)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.MouseEnter += item_MouseEnter;

                if (table.IsVirtual == false)
                {
                    item.Content = "[" + table.TableDescription + "]";
                }
                else
                {
                    item.Content = "[" + table.View.Name + "]";
                }

                item.Tag = table;
                cmb.Items.Add(item);
            }
        }

 private void item_MouseEnter(object sender, MouseEventArgs e)
        {
            ComboBoxItem item = sender as ComboBoxItem;

            //Do task
        }

2 个答案:

答案 0 :(得分:0)

尝试GotFocus事件。即使用户使用键盘选择项目,也会触发。我相信这就是你真正想要的:)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var comboBoxItem1 = new ComboBoxItem();
        var comboBoxItem2 = new ComboBoxItem();

        comboBoxItem1.Content = new Button() { Content = "button1" };
        comboBoxItem1.GotFocus += (sender, args) => lbl1.Content = "1111";
        comboBoxItem2.Content = "222222";
        comboBoxItem2.GotFocus += (sender, args) => lbl1.Content = "2222";

        comboBox1.Items.Add(comboBoxItem1);
        comboBox1.Items.Add(comboBoxItem2);
    }
}

enter image description here

enter image description here

答案 1 :(得分:0)

您是否意外删除了designer.cs上的事件?