ComboBoxItem MouseEnter事件未触发

WPF*_*WPF 6 .net c# windows wpf events

我有以下示例代码.奇怪的是,MouseMove事件正常启动,但是当替换时MouseEnter,当鼠标移过时,没有任何反应ComboBoxItem.知道如何解决这个问题吗?我实际上需要在用户将鼠标悬停在a上时发生事件ComboBoxItem,以及当悬停离开项目时的另一个事件.

var comboBoxItem1 = new ComboBoxItem();
var comboBoxItem2 = new ComboBoxItem();
cmb.Items.Add(comboBoxItem1);
cmb.Items.Add(comboBoxItem2);

comboBoxItem1.Content = "1";

comboBoxItem1.MouseMove += (s, args) =>
{
    MessageBox.Show("1");
};

comboBoxItem2.Content = "2";
comboBoxItem2.MouseMove += (s, args) =>
{
    MessageBox.Show("2");
};
Run Code Online (Sandbox Code Playgroud)

编辑:

                StackPanel spCondition = new StackPanel();
                spCondition.Orientation = Orientation.Horizontal;

                ComboBox cmbValue1 = new ComboBox();
                cmbValue1.IsTextSearchEnabled = false;
                cmbValue1.IsEditable = true;
                cmbValue1.Width = 70;
                cmbValue1.LostFocus += cmbValue_LostFocus;
                cmbValue1.PreviewMouseLeftButtonDown += cmbValue_MouseLeftButtonDown;
                cmbValue1.SelectionChanged += cmbValue_SelectionChanged;

                Border border = new Border();
                border.Child = cmbValue1;

                spCondition.Children.Add(border);   

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 parentTable.ParentVisualQueryBuilder.ListOpenUnjoinedTables)
        {
            ComboBoxItem item = new ComboBoxItem();
            item.MouseMove += item_MouseMove;

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

            item.Tag = table;
            cmb.Items.Add(item);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Dil*_*hod 0

我确信您正在删除此代码中 ComboBox 中的项目:

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 parentTable.ParentVisualQueryBuilder.ListOpenUnjoinedTables)
        {
            ComboBoxItem item = new ComboBoxItem();
            item.MouseMove += item_MouseMove;

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

            item.Tag = table;
            cmb.Items.Add(item);
        }
    }
Run Code Online (Sandbox Code Playgroud)

请尝试注释此代码并运行。

更新:

您可以使用以下代码将项目添加到 comoBox:

var comboBoxItem1 = new ComboBoxItem();
var comboBoxItem2 = new ComboBoxItem();
cmb.Items.Add(comboBoxItem1);
cmb.Items.Add(comboBoxItem2);

comboBoxItem1.Content = "1";

comboBoxItem1.MouseMove += (s, args) =>
{
    MessageBox.Show("1");
};

comboBoxItem2.Content = "2";
comboBoxItem2.MouseMove += (s, args) =>
{
    MessageBox.Show("2");
};
Run Code Online (Sandbox Code Playgroud)

用这段代码改变它。

var comboBoxItem1 = new Label();//or use textBolck
var comboBoxItem2 = new Label();//or use textBolck
combo.Items.Add(comboBoxItem1);
combo.Items.Add(comboBoxItem2);

comboBoxItem1.Content = "1";

comboBoxItem1.MouseEnter += (s, args) =>
{
     MessageBox.Show("1");
};


comboBoxItem2.Content = "2";
comboBoxItem2.MouseEnter += (s, args) =>
{
     MessageBox.Show("2");
};
Run Code Online (Sandbox Code Playgroud)