Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I created a window phone application in-which I created a Image control from the code behind in my window phone application.. I placed all the automatically generated image control in a stack-panel, i added a hold event to the image control, but the problem is how do i access the properties of the selected control that fired the event.. Thanks

C#
private void ProcessPhoto(PhotoResult phtres)
        {
            if (phtres.TaskResult == TaskResult.OK && phtres.ChosenPhoto != null)
            {
                for (int i = 0; i < arrstream.Count(); i++)
                {
                    if (arrstream[i] == null)
                    {
                        arrstream[i] = phtres.ChosenPhoto;
                        picocunt++;
                        WriteableBitmap wb = new WriteableBitmap(PictureDecoder.DecodeJpeg(arrstream[i]));
                        var img = new Image();
                        img.Name = "picbox" + i.ToString();
                        img.Height = 80;
                        img.Width = 80;
                        img.Margin = new Thickness(0, 0, 5, 0);
                        img.Source = wb;
                        Photo_Pan.Children.Add(img);
                        img.Hold += new EventHandler<System.Windows.Input.GestureEventArgs>(img_Hold);
                        break;
                    }
                }
                ArrCount.Text = string.Format("{0} Media Slot Remaining", arrstream.Length - picocunt);

                progressBar1.Visibility = Visibility.Collapsed;

                
            }
        }

//Event to be fired when any of the image control is pressed..
        void img_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //Image img = new Image();
            //Photo_Pan.Children.IndexOf(img);
            //MessageBox.Show(();
        }


Above is my code sample
Posted
Updated 4-Jul-12 9:43am
v2
Comments
Sergey Alexandrovich Kryukov 4-Jul-12 15:36pm    
What is your problem? I wonder, how can it be a problem, taking into account that you already successfully created and added controls to your UI?
--SA
IamBlessed 4-Jul-12 15:38pm    
The problem is that i want want a user select any of the programatically created image controls it fires a event, i also want to be able to get the particular control that triggered the event
[no name] 4-Jul-12 17:15pm    
The control that fired the event is passed as the sender parameter.

1 solution

This is quite an artificial problem. How come it could be a problem? If you need to access to something, you need to address it by name, right? But it does not have to be direct. As you create and insert controls somewhere in the very beginning, but you need to access them from time to time, those names should be the names of some class/structure instance members, right? Most natural place should be the class of your window or a form which host your stack panel with forms.

So, when you first create a control, its constructor always return the reference to it. You already used this reference to insert a control in a panel. Assign this reference to some member of your class, a field or a property; it depends on how you want to use it.

It was about direct access. This is not what you always want. Sometime you have many controls of the similar purpose which you want to keep in an array or some other collection. In this way, you should only keep a reference to the array of some collection as an instance member of your class. In this case, the references to individual controls will be accessed by index, or by some key, if this is a key-value based collection. Please see:
http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx[^],
http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900