-1

In C# WPF, I'm trying to create a new picturebox on the point where I clicked, whenever I click on the main window. I'm not too sure how to approach this since I could not find anything on the internet about this.

4

1 回答 1

0

Are you sure you want to create new one each time? That could potentially be a bad idea. But still, if you want to, You can do it in codebehind like this:

    public MainWindow()
    {
        InitializeComponent();
        MouseUp += MainWindow_MouseUp; //add eventhandler vor click event
    }

    void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
    {
        var img = new Image(); //create new instance of image
        img.Width = 100; //set some size properties
        img.Height = 100;
        img.Source = somesource;//set source
        MainGrid.Children.Add(img); //add it as a child to some conteiner element, like grid. 
    }

But please reconsider reusing controls, if possible.

于 2015-03-23T00:39:36.950 回答