访问子控件 [英] Accessing child control

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

问题描述

让我更详细地尝试一下.谢谢您到目前为止对克里斯蒂安的帮助,但我相信我会带您走错路.我很抱歉.

一些背景信息,这是一个WPF应用程序,具有WrapPanel来保存在运行时添加到其中的多个用户控件.面板开始为空.有一个图标列表,代表用户可以单击的各种形状.单击图标后,它将形状(实际上是代表该形状的MoveBlock用户控件)添加到WrapPanel.允许用户使用每个形状中的多个形状,并且可以添加/删除/重新排列形状以创建图案.

现在,我正在测试实现.我在主窗口上有一个测试按钮,其中包含以下代码:

Let me try this with a little more detail. Thank you for your help so far Christian, but I believe I''m leading you down the wrong road. My apologies.

Some background info, this is a WPF application that has a WrapPanel to hold several user controls that are added to it at runtime. The panel starts empty. There is a list of icons representing various shapes the user can click on. When an icon is clicked, it addes the shape (actually a MoveBlock user control representing the shape) to the WrapPanel. The user is allowed to have more than one of each shape and can add/remove/reorder the shapes to create a pattern.

Right now I''m testing my implementation. I have a test button on the main window with the following code:

Controls.MoveBlock newMove = new Controls.MoveBlock();
newMove.Name = "Move1";
newMove.Width = 96;
newMove.Height = 96;
newMove.Highlight = Controls.MoveBlockHighlight.Selected;
... other settings
MovesWrapPanel.Children.Add(newMove);
newMove = new Controls.MoveBlock();
newMove.Name = "Move2";
... other settings
MovesWrapPanel.Children.Add(newMove);



很好我可以在WrapPanel中看到控件,它们的行为与预期的一样.但是,我无法使用以下代码删除该控件:



That works fine. I can see the controls in the WrapPanel and they act like they are suppose to. However, I cannot remove the control using the following code:

MovesWrapPanel.Children.Remove((Controls.MoveBlock)MovesWrapPanel.FindName("Move1"));


但是,如果我使用设计器创建了控件并将其命名为"Move1",则该代码将起作用.

为了弄清楚发生了什么,我有一个MessagBox,它显示了单击该控件时的名称.


But the code will work if I created the control using the designer and named it "Move1".

In an effort to figure out what''s going on, I have a MessagBox showing the name of the control when it is clicked.

MessageBox.Show("Name is " + this.Name);


单击"Move1"显示名称为Move1",单击"Move2"显示名称为Move2".

克里斯蒂安(Christian),您提到它可能正在使用另一个属性,特别是ID.这听起来像是一个很好的解释,但我找不到具有与 MoveBlock.Name 值匹配的值的其他任何属性.

我正在尝试删除WrapPanel的特定子项.每个孩子都是我创建的用户控件MoveBlock.用户可以在运行时添加和删除它们.

[更新]我正在寻找删除用户选择的特定项目的功能.可能有10个项目,用户想要删除第4个项目.而且,用户可以对控件进行重新排序.它可能以123456开始,但在用户完成操作时为4631(删除了两个)或53271(添加了另一个).然后,我需要按照显示的顺序浏览列表,以制作一个文件来创建显示的零件.

我正在使用:


Clicking Move1 shows "Name is Move1" and clicking Move2 shows "Name is Move2".

Christian, you mentioned that it could be using another property, specifically the ID. That sounds like a great explanation, but I can''t find any other property that has a value that matches the MoveBlock.Name value.

I''m trying to delete specific children of a WrapPanel. Each child is a user control I created, MoveBlock. They are added and removed at runtime by the user.

[UPDATED] I''m looking for the ability to remove a specific item that is selected by the user. There may be 10 items and the user wants to remove the 4th one. Also, the user has the ability to reorder the controls. It may start of 123456, but be 4631 (removed two) or 53271 (added another) by the time the user is done. I then need to go through the list in the order displayed to make a file to create the part shown.

I''m using:

MovesWrapPanel.Children.Remove((Controls.MoveBlock)MovesWrapPanel.FindName("Move1"));



只要在使用设计器添加时将控件命名为"Move1",此方法就可以正常工作.

但是,如果我不给控件一个名称并将其设置为"Move1",就可以通过在运行上述行之前在运行时设置控件的Name属性来将其更改为"Move1":



This works fine as long as I named the control "Move1" when I added using the designer.

However, it doesn''t if I don''t give the control a name and change it to "Move1" by setting the Name property of the control at runtime before running the line above:

private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
    {
      this.Name = "Move1";
    }



我知道这是一个基本问题,但是我找不到运气的答案.

谢谢,



I know this is a basic question, but I have had no luck finding the answer.

Thanks,

推荐答案

代替使用findname,您可以遍历子级并使用"as"关键字,如

MoveBlock mv =子级为MoveBlock;

此时,如果mv不为null,则它是一个移动块,因此您可以将其删除.
Instead of using findname, you can iterate over the children and use the ''as'' keyword, as in

MoveBlock mv = child as MoveBlock;

at this point, if mv is not null, it was a moveblock, so you can delete it.


我怀疑它是ID属性,而不是正在使用的name属性搜索,即使它名为FindName.您是否尝试设置ID?如果不是,那么下一个问题是,如果您使用我给您的代码遍历它们,它们具有什么名称/ID?


I suspect that it''s the ID property and not the name property that is being searched for, even though it''s called FindName. Did you try setting the ID ? If not, the next question is, if you iterate over them using the code I gave you, what name/ID DOES it have ?


写道:​​

私有无效UserControl_MouseUp(对象发送方,MouseButtonEventArgs e){this.Name ="Move1"; }

private void UserControl_MouseUp(object sender, MouseButtonEventArgs e) { this.Name = "Move1"; }



这是一个想法-您为什么要在服务器上执行此操作,此代码与其他代码之间的操作顺序是什么?



Here''s a thought - why are you doing this on the server, and what is the order of operations between this and the other code ?


哦-对不起.在这种情况下,您要删除的对象上的click事件将传递给sender属性.您可以将其强制转换为您的类类型(单击的是控件),因此可以从那里将其删除,因此名称是无关紧要的.出于某些奇怪的原因,我以为您在问一个ASP.NET问题.
Oh - sorry. In that case, the click event on the object you want to remove, is passed the sender property. You can cast this to your class type ( it is the control that was clicked ) and so you can remove it from there, the name is then irrelevant. For some bizarre reason, I thought you were asking an ASP.NET question.


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

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