3

I'm trying to add controls dynamically to a wrap panel on a window but after two wrap panel controls are added to the original wrap panel control it doesn't add anymore here is the code im using to add the image

 Random rn = new Random();
 ImageContainer.Children.Add(displayimage(rn.Next(amount)));            
 ImageContainer.InvalidateVisual();

im new to wpf and just wondering if im doing something wrong or missing something.

any help would be greatly appreciated

EDIT

        public WrapPanel displayimage(int i)
       {

        WrapPanel pn = new WrapPanel();
        pn.Width = 350;
        pn.Height = 400;
        pn.Background = new SolidColorBrush(Colors.White);
        BitmapImage bm = new BitmapImage(new Uri(imagePaths[i]));
        Image im = new Image();
        im.Source = bm;
        im.Height = 300;
        im.Width = 400;
        im.Margin = new Thickness(25,25,25,25);
        pn.Children.Add(im);
        pn.Margin = Location(pn);
        pn.ClipToBounds = true;

        return pn;

    }
9
  • Fist: if you use the WPF you should use the XAML too, if you don't you miss a impotent thing. Second: If i understood this right you try to add controls Dynamic into a WrapPanel?that means if you hit the "Add" button there is always another Control that will be add? please explain what exactly you try to do. Commented Jan 20, 2013 at 19:47
  • I'm creating trying to add images to my wrap panel in random places on my screen but the most it will ever add is two images the images are added every 5 seconds Commented Jan 20, 2013 at 19:51
  • @Venson How do you conclude that there is no XAML? And what Add button are you talking about? Commented Jan 20, 2013 at 19:57
  • @RoyJamesSchumacher You would have to add more context to you question. Where is your code called? When you say "every 5 seconds", is there some timer running that calls it? And what does the displayImage method look like? Commented Jan 20, 2013 at 19:58
  • @Clemens that button was just for example. And i am sure becorse of this code snipping: ImageContainer.Children.Add... Commented Jan 20, 2013 at 20:02

1 Answer 1

6

In order to put images at random places in a container control, you should not use a WrapPanel, but a Canvas instead. Canvas is made for absolute positioning of elements. You set the position of a child element of a Canvas by setting the Canvas.Left and Canvas.Top properties (or Canvas.Right or Canvas.Bottom).

Moreover you don't need any "inner" panel, as Image is a control that can be added directly to any container.

So change your displayimage method like this:

public UIElement GetDisplayImage(int i)
{
    var bm = new BitmapImage(new Uri(imagePaths[i]));
    var im = new Image
    {
        Source = bm,
        Height = 300,
        Width = 400
    };
    var location = Location(im);
    Canvas.SetLeft(im, location.X);
    Canvas.SetTop(im, location.Y);
    return im;
}

Now add these Images to a Canvas:

Random rn = new Random();
ImageCanvas.Children.Add(GetDisplayImage(rn.Next(amount));

The InvalidateVisual is not necessary.


You might also have to take care that images aren't added multiple times, since Random.Next may return the same number multiple times.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much this is doing exactly what i want it to do you have helped a lot, as for the same number being returned multiple times I'm not to worried about that as it I'm allowing my images to overlap as they are added. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.