WPF FindName问题 [英] WPF FindName problem

查看:337
本文介绍了WPF FindName问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我不知道我的代码有什么问题,我将图像添加到myGrid
并命名我的图片,当我想按名称查找我的图片时,这是我的代码:

我先做:

Hi
I don''t know what is the problem with my code I add an Image to myGrid
and name my image and when I want to find my Image by name, here is my code:

I first do :

myControlIcons[p, q].Source = myBitmapImage;
                   myControlIcons[p, q].Name = "ControlIcon_" + p.ToString() + "_" + q.ToString();
                   myGrid.Children.Add(myControlIcons[p, q]);


然后当我遇到问题时


and then here when I got problem

object myFE = myGrid.FindName("ControlIcon_" + p.ToString() + "_" + q.ToString());
               if (myFE != null)
                   MessageBox.Show("it is framework element");
               if(myFE == null)
                   MessageBox.Show("it is not framework element");

推荐答案

我自己找到了答案
答案是应该注册所有名称,以便解决方案:
I found the answer myself
the answer is that all names should be registered so the solution:
Grid myGrid = new Grid();
Image myImage = new Image();
myImage.Name = "myImage";

//here 
NameScope.SetNameScope(myGrid, new NameScope());

myGrid.RegisterName(myImage.Name, myImage);


这篇关于WPF FindName问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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