我有以下示例代码。奇怪的是,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);
}
}
答案 0 :(得分: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);
}
}
请尝试评论此代码并运行。
更新:
您可以使用此代码将项目添加到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");
};
答案 1 :(得分:0)
尝试使用PreviewMouseEnter事件。因为我猜测有一些元素可以窃取事件,所以使用隧道事件应该会有所帮助。