C # WPF Want Stackpanel Child to Click
I am using StackPanels. In my application I need to show several tiffs with images from 3 to x and open them in a new window after I click on one of them.
It's easy to show them:
public void Bilder_anzeigen(string path)
{
TiffBitmapDecoder decoder = new TiffBitmapDecoder(new Uri(path), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
foreach (var i in decoder.Frames)
{
Image myImage = new Image();
myImage.Source = i;
Stackpanel_Tiff.Children.Add(myImage);
}
}
But how can I get a StackPanel clicked child? There is a MouseDown event, but after raising it, I don't know which image I clicked on. I just know there was a click. How can I find a found image?
source to share
You can find out which one Image
was clicked very easily using event PreviewMouseDown
and OriginalSource
object MouoseButtonEventArgs
:
<StackPanel PreviewMouseDown="StackPanel_PreviewMouseDown">
<Image Source="Images/Add_16.png" Stretch="None" />
<Image Source="Images/Edit_16.png" Stretch="None" />
<Image Source="Images/Copy_16.png" Stretch="None" />
</StackPanel>
...
private void StackPanel_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource is Image)
{
string imageSource = ((Image)e.OriginalSource).Source.ToString();
}
}
source to share
You can just use event Image.MouseDown
or Image.MouseUp
.
Alternatively, take control of the mouse cursor .
source to share