简体   繁体   English

访问列表中的UIElement.Properties <UIElement>

[英]Access to UIElement.Properties in a List<UIElement>

I'm creating multiple Canvas in run time, and each Canvas has a TextBox as Children. 我在运行时创建多个Canvas ,每个Canvas都有一个TextBox作为Children。 These Canvas are Children of a WrapPanel . 这些CanvasWrapPanel孩子。 My code is as follow: 我的代码如下:

List<Canvas> cvList = new List<Canvas>();
WrapPanel wp = new WrapPanel();

// function (e.g. a Click event) to add a Canvas with a TextBox
Canvas cv = new Canvas();
TextBox tb = new TextBox();
cv.Children.Add(tb);
cvList.Add(cv);
wp.Children.Add(cv);

If after the function is processed 10 times and 10 Canvas is create, I need to change the Text of one specific UIElement (eg the 5th TextBox). 如果在处理函数10次并创建10个Canvas后,我需要更改一个特定UIElement的文本(例如第5个TextBox)。 How would I do so? 我该怎么办? There is no index to use like that in an array. 在数组中没有像这样使用的索引。

[Edit] If I have a button click event also generated in run-time, each button will close its corresponding Canvas: [编辑]如果我还在运行时生成按钮单击事件,则每个按钮将关闭其对应的Canvas:

List<Button> btnList = new List<Button>();

// function to add Canvas with a Button
Button btn = new Button();
btn.Content = "Destroy";
btn.Click += destroy_Click;
btnList.Add(btn);
cv.Children.Add(btn);

// Destroy click event
private void destroy_Click(object sender, RoutedEventArgs e)
{
if (sender == ????)
{}
}

What should I compare my sender to? 我应该如何比较我的寄件人?

As I was trying to explain in your last question (which you probably just should have updated) you don't need a separate list, just use the WrapPanel directly to access its children - You know you only added Canvas elements to your WrapPanel so you can cast appropriately: 当我试图解释你的上一个问题 (你可能应该更新)时,你不需要单独的列表,只需直接使用WrapPanel访问它的子项 - 你知道你只在你的WrapPanel添加了Canvas元素,所以你可以适当投射:

TextBox textbox = (wp.Children[5] as Canvas).Children[0] as TextBox;
textbox.Text = "Hello";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM