如何访问以编程方式创建的控件 [英] How to get access to controls created programmatically

查看:67
本文介绍了如何访问以编程方式创建的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们

我创建了一个窗口电话应用程序,在其中,我从窗口电话应用程序中的代码创建了一个Image控件..我将所有自动生成的图像控件放置在堆栈面板中,向该图像控件添加了一个hold事件,但是问题是我如何访问触发事件的所选控件的属性..谢谢

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

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

推荐答案

这是一个人为的问题.怎么可能是个问题?如果您需要访问某些内容,则需要按名称进行寻址,对吗?但这并不一定是直接的.当您在一开始就在某个位置创建并插入控件时,但是您需要不时访问它们时,这些名称应该是某些类/结构实例成员的名称,对吗?最自然的地方应该是窗口的类或使用窗体托管堆栈面板的窗体.

因此,当您第一次创建控件时,其构造函数始终会返回对该控件的引用.您已经使用此参考在面板中插入控件.将此引用分配给您的类的某个成员,字段或属性;这取决于您要如何使用它.

这是关于直接访问.这不是您一直想要的.有时候,您有许多出于类似目的的控件要保留在数组或其他集合中.这样,您应该只保留对某个集合的数组的引用作为类的实例成员.在这种情况下,对单个控件的引用将通过索引或某个键(如果这是基于键值的集合)进行访问.请参阅:
http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.collections. generic.aspx [ ^ ].

—SA
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


这篇关于如何访问以编程方式创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆