我做了2个MouseEvents,他们工作,但问题是,他们的工作不像我预期的那样。当我的鼠标指针在网格空间中时,我需要激活这两个事件,但现在它们仅在指针位于任何一条线上时才有效。
我的代码:
// Grid 3 Rows.
Grid grid_Edit = new Grid();
Grid.SetRow(grid_Edit, 0);
Grid.SetColumn(grid_Edit, 1);
RowDefinition rowDef1 = new RowDefinition();
RowDefinition rowDef2 = new RowDefinition();
RowDefinition rowDef3 = new RowDefinition();
grid_Edit.RowDefinitions.Add(rowDef1);
grid_Edit.RowDefinitions.Add(rowDef2);
grid_Edit.RowDefinitions.Add(rowDef3);
grid_Edit.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Star);
grid_Edit.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
grid_Edit.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
grid_Edit.MouseEnter += new MouseEventHandler(gridEdit_MouseEnter);
grid_Edit.MouseLeave += new MouseEventHandler(gridEdit_MouseLeave);
mainWindow_ref.Children.Add(grid_Edit);
// 3 lines
line1.Stroke = Brushes.White;
line1.X1 = 1;
line1.Stretch = Stretch.Fill;
Grid.SetRow(line1, 0);
line1.VerticalAlignment = VerticalAlignment.Center;
line2.Stroke = Brushes.White;
line2.X1 = 1;
line2.Stretch = Stretch.Fill;
Grid.SetRow(line2, 1);
line2.VerticalAlignment = VerticalAlignment.Center;
line3.Stroke = Brushes.White;
line3.X1 = 1;
line3.Stretch = Stretch.Fill;
Grid.SetRow(line3, 2);
line3.VerticalAlignment = VerticalAlignment.Center;
// add lines to grid_Edit
grid_Edit.Children.Add(line1);
grid_Edit.Children.Add(line2);
grid_Edit.Children.Add(line3);
private static void gridEdit_MouseLeave(object sender, MouseEventArgs e)
{
line1.Stroke = Brushes.White;
line2.Stroke = Brushes.White;
line3.Stroke = Brushes.White;
}
private static void gridEdit_MouseEnter(object sender, MouseEventArgs e)
{
line1.Stroke = Brushes.Black;
line2.Stroke = Brushes.Black;
line3.Stroke = Brushes.Black;
}
答案 0 :(得分:3)
将网格的背景颜色设置为透明,这将允许网格捕获鼠标事件。