C# WPF add control to main window at run time

前端 未结 3 1205
长发绾君心
长发绾君心 2021-01-11 11:42

My goal is to attach a new image control while the application is running.

img = new System.Windows.Controls.Image();
img.Margin = new Thickness(200, 10, 0,          


        
3条回答
  •  無奈伤痛
    2021-01-11 12:07

    You should have only one root element under window. Adding the image using this.AddChilda adds the image as child of window, but you probably have some other child defined(Grid for example). Give a name to this child (Grid in the example) and then in the code behind add the image to the Grid

    Example :

    
    
    
    
    
    

    Then in the code behind use

    RootGrid.Children.Add(img);
    

提交回复
热议问题