简体   繁体   English

如何在事件可见的情况下制作画布?

[英]How can i make the Canvas in a event visible?

i have following problem, i want to generate a little canvas by a button click, after generating i want to move it by a key press event but i cant see the canvas in the event. 我有以下问题,我想通过按钮点击生成一个小画布,生成后我想通过按键事件移动它但我无法在事件中看到画布。 How can i make it visible? 我怎样才能看到它? (In the sourcecode of WPF not in XAML) (在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" :(          
}

使用参数发送者:

Canvas ship = (Canvas) sender;

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM