15

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, 0);
img.Width = 32;
img.Height = 32;
img.Source = etc;

I've tried

this.AddChild(img);// says must be a single element
this.AddLogicalChild(img);// does nothing
this.AddVisualChild(img);// does nothing

It was never this difficult to add a element with forms. How can I simply attach this new element to the main window (not another control) so that it will show up.

Solved it, I named the grid main, and from there I was able to access the children attribute and the add function

main.children.add(img);

<Window x:Class="Crysis_Menu.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" Loaded="Window_Loaded" AllowsTransparency="False" Background="White" Foreground="{x:Null}" WindowStyle="SingleBorderWindow">
    <Grid Name="main">
        <Button Content="Run" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="btnRun" VerticalAlignment="Top" Width="151" Click="btnRun_Click" />
        <TextBox Height="259" HorizontalAlignment="Left" Margin="12,40,0,0" Name="tbStatus" VerticalAlignment="Top" Width="151" />
    </Grid>
</Window>
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Drake
  • 3,851
  • 8
  • 39
  • 48

3 Answers3

15

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 :

<Window>
<Grid x:Name="RootGrid">

</Grid>
</Window>

Then in the code behind use

RootGrid.Children.Add(img);
Clemens
  • 123,504
  • 12
  • 155
  • 268
Kiril Popov
  • 376
  • 2
  • 9
6

What is this in your case? You can try this.Content = image; or this.Children.Add(image);

If your this is indeed a Window, you should know that Window can have only a single child, which you put into Content. If you want several items in Window, usually you put some appropriate container (for example, Grid or StackPanel) as Window's content, and add children to it.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • This is the main window: http://screensnapr.com/v/OROEvt.png it has no children attribute. I need to add it to the grid, the element that is holding the button and text box you see in this picture – Drake Oct 14 '11 at 21:37
  • Yes, Window has only Content. What is your Window's content than? You should add not to the window, but to the appropriate inner container. That's how the layout management works :-) – Vlad Oct 14 '11 at 21:39
2

Vlad got the solution. I used it :

var grid = this.Content as Grid;

// or any controls
Label lblMessage = new Label
{
    Content = "I am a label",
    Margin = new Thickness(86, 269, 0, 0)
};

grid.Children.Add(lblMessage);
aloisdg
  • 22,270
  • 6
  • 85
  • 105