ComboBoxItem鼠标进入事件未触发

6
我有以下样例代码。奇怪的是,MouseMove事件正常触发,但当用MouseEnter替换时,当鼠标移动到ComboBoxItem上时什么也不发生。有什么办法可以解决这个问题吗?我需要一个事件在用户悬停在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");
};

编辑:

                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);
        }
    }

你能否尝试在MessageBox.Show这一行上设置一个断点,看看它是否会停在那里? - Yahya
@Yahya - 甚至没有进入代码块! - WPF
1
“Windows事件”对我来说意味着其他东西。我已经为您更正了标题。 - spender
cmb的父元素有背景吗? - skink
@Joulukuusi 你是什么意思? - WPF
显示剩余13条评论
2个回答

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);
        }
    }

请尝试注释此代码并运行。
更新:
您有这段代码来向下拉框中添加项目:
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");
};

用这段代码替换它。

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");
};

是的,你说得对。首先你删除了所有的项目并向comboBox添加了新项目。在这种情况下,你将无法看到MessegeBox。 - Dilshod
您上传的项目在cmbValue_MouseLeftButtonDown()中没有任何代码。请尝试运行您上传的项目。 - Dilshod
我尝试运行已上传的项目,但当MouseMove被替换为MouseEnter时,它无法正常工作。 - WPF
如果我理解正确的话,您想在鼠标进入ComboBoxItem时看到消息框。是这样吗? - Dilshod
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/26560/discussion-between-dilshod-and-wpf - Dilshod
显示剩余2条评论

0
尝试使用PreviewMouseEnter事件。因为我猜测有一些元素会窃取该事件,所以使用隧道事件应该会有所帮助。

ComboBoxItem似乎不存在PreviewMouseEnter事件。 - WPF

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接