板类中的wpf MouseMove-Event不起作用

时间:2013-04-20 17:00:08

标签: wpf events mousemove

我正在写一个程序,目前有两个不同的类。其中一个是所谓的“板”类,另一个是“MainWindow”类。

MainWindo的xaml代码如下所示。

<Window x:Class="Game.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" SizeChanged="MainWindowSizeChanged">
<Grid Name="grid">

    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

</Grid>

“board”类有一个用标签填充网格的功能:

public void CreateBoard(Grid grid)
    {
        int Rows = grid.RowDefinitions.Count;
        int Cols = grid.ColumnDefinitions.Count;


        BlockControls = new Label[Cols, Rows];
        for (int i = 0; i < Cols; i++)
        {
            for (int j = 0; j < Rows; j++)
            {
                BlockControls[i, j] = new Label();
                BlockControls[i, j].Background = Brushes.Transparent;
                BlockControls[i, j].BorderBrush = Brushes.Violet;
                BlockControls[i, j].BorderThickness = new Thickness(1, 1, 1, 1);

                Grid.SetRow(BlockControls[i, j], j);
                Grid.SetColumn(BlockControls[i, j], i);
                grid.Children.Add(BlockControls[i, j]);
            }
        }
    }

此函数将在MainWindows类中调用

Board myboard = new Board();
myboard.CreateBoard(grid);

现在我想做点如下的事情:

myboard.BlockControls[0, 0].MouseMove += new MouseEventHandler(MouseMoveEvent);

但这没有用,因为什么都没发生,为什么?

MouseMoveEvent函数如下:

private void MouseMoveEvent(object sender, MouseEventArgs e)
    {
        Console.WriteLine("MouseMove");
    }

提前致谢

1 个答案:

答案 0 :(得分:0)

谢谢你的回答。我在board类中创建了一个Label来检查MouseMove事件是否正常工作。

在董事会班级:

public Label label = new Label();

在MainWindo类中:

myboard.label.Height = 100;
myboard.label.Width = 100;         
myboard.label.Background = Brushes.Aqua;
myboard.label.Margin = new Thickness(10, 0, 10, 5);

grid.Children.Add(myboard.label)
myboard.MouseMove += new MouseEventHandler(MouseMoveEvent);

我仍有同样的问题,所以我尝试过这样的问题:http://www.codeproject.com/Questions/287115/Mouse-click-events-for-Rectangle-or-ellipse-drawn

但这也不起作用。看来你是对的,它必须对RoutedEvent做一些事情,因为如果我将上面的代码更改为:

myboard.label.Height = 100;
myboard.label.Width = 100;         
myboard.label.Background = Brushes.Aqua;
myboard.label.Margin = new Thickness(10, 0, 10, 5);

this.Content = myboard.label;

它工作正常,只是它只显示新标签,而不是网格和其余部分。

任何线索,我能做什么?

由于