gpt4 book ai didi

c# - ComboBoxItem MouseEnter 事件未触发

转载 作者:可可西里 更新时间:2023-11-01 13:27:36 25 4
gpt4 key购买 nike

我有以下示例代码。奇怪的是,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);
}
}

最佳答案

我确定您要在这段代码中删除 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);
}
}

请尝试注释这段代码并运行。

更新:

您有这段代码可以将项目添加到 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");
};

用这段代码改变它。

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

关于c# - ComboBoxItem MouseEnter 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15525704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com