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?

+3


source to share


5 answers


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();
    }
}

      

+5


source


Try the original event source. OriginalSource gives the control on which MouseDown



       private void Sp_MouseDown_1(object sender, MouseButtonEventArgs e)
    {
        var image=e.OriginalSource as Image;
    }

      

+3


source


In your StackPanel MouseDown event, you can try:

if (e.OriginalSource is Image)
{
    var tapImage = (Image)e.OriginalSource;
    //tapImage is the Image on which user tapped.
}

      

see if that helps.

+3


source


You can just use event Image.MouseDown

or Image.MouseUp

.

Alternatively, take control of the mouse cursor .

+2


source


while it works.

 image.MouseDown += (e, v) => { //enter your code };

      

I would recommend that you use MVVM and bind the command using CommandBinding.

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

0


source







All Articles