0

我有以下问题,我想通过单击按钮生成一个小画布,生成后我想通过按键事件移动它,但我无法在事件中看到画布。我怎样才能让它可见?(在 WPF 的源代码中,不在 XAML 中)

public void Button_Click_1(object sender, RoutedEventArgs e)
{   
    Canvas c = new Canvas();
    c.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
    c.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
    c.Loaded += c_Loaded;
    Grid.Children.Add(c);

    Canvas ship = new Canvas();
    ship.Background = Brushes.Aquamarine;
    ship.Height = 30;
    ship.Width = 30;
    ship.KeyDown += ship_KeyDown;
    Canvas.SetTop(ship, 50);
    c.Children.Add(ship);  
}

void ship_KeyDown(object sender, KeyEventArgs e)
{
    canvas.Setleft(ship, canvas.Getleft(ship) +10);  //here i can not see the object "ship" :(          
}
4

3 回答 3

2

使用参数发送者:

Canvas ship = (Canvas) sender;
于 2013-01-11T14:07:53.587 回答
0

您需要添加 c 或运送到 layoutroot`

于 2013-01-11T14:07:07.277 回答
0

最简单的方法是将其添加到 a <list>ofUIElement并使用foreach-loop对其进行修改

于 2013-05-29T09:10:09.383 回答