Chad
Chad

Reputation: 163

C# add a click event to grid childs

How can I add a click event to child objects I have created in my Code Behind?

String[] imageNames = System.IO.Directory.GetFiles("Assets/Images/Gallery");

foreach (String image in imageNames)
{
    Uri source = new Uri("ms-appx:///" + image);

    ImageSource imgSource = new BitmapImage(source);

    Image myImage = new Image();
    myImage.Source = imgSource;

    gallery.Children.Add(myImage);    
}

The above code gets all the images from the gallery folder and adds them to a variablesizedwrapgrid. I want to add a click event on each of these (to basically allow a user to save them to the device they are using)

Thanks

Upvotes: 1

Views: 503

Answers (1)

RTDev
RTDev

Reputation: 866

1)

myImage.PointerReleased += Img_PointerReleased;

private void Img_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    // do your job
}

2) or write UserControl wrapping Image that inherits from Button and add proper VisualStates if you don't want to have default button animations - this way you will be able to use Clicked event

Upvotes: 1

Related Questions