2013-01-11 20 views
0

我有以下問題,我想通過按鈕單擊生成一個小畫布,產生後我想通過按鍵事件移動它,但我不能在事件中看到畫布。我怎樣才能讓它可見? (在WPF的不是在XAML的源代碼)如何使Canvas在事件中可見?

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" :(   
} 

回答

0

您需要您C或船添加到layoutroot`

+0

如何將它們添加到layoutroot中? – Mike

2

使用參數發件人:

Canvas ship = (Canvas) sender; 
+0

我應該在哪裏附上這段代碼? – Mike

+0

在ship_KeyDown – chameleon86

+0

beginnig如果我附上,我不能移動它或給它一個其他的背景或東西.. – Mike

0

最簡單的方法是將其添加到<list>UIElement並將其修改爲foreach -loop

相關問題