动态的和不可改变的UIElement阵列 [英] Dynamic and Immutable UIElement Arrays

查看:163
本文介绍了动态的和不可改变的UIElement阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WrapPanel 包含多个大小相同的画布。每个画布有一些的UIElement S(即文本框的TextBlock 按钮取值等)的孩子。创建每个画布(包括其的UIElement 儿童)和画布<数/ code>来创建背后(不XAML)运行时code都做。

I have a WrapPanel that contains multiple Canvas of the same size. Each Canvas has some UIElements (i.e. TextBox, TextBlock, Buttons etc) as children. The creation of each Canvas (including its UIElement children) and the number of Canvas to be created are all done in run-time code behind (no XAML).

起初,我做了以下内容,其工作方式:

Initially I did the following, which works:

// declare as class properties, so all function can access them
WrapPanel wp = new WrapPanel();
Canvas[] cv = new Canvas[500];
TextBox[] tb = new TextBox[500];

// A function (e.g. a Button_Click event) that generates multiple Canvas in a WrapPanel
for (int i = 0; i<myInt; i++)
{
cv[i] = new Canvas();
tb[i] = new TextBox();
cv[i].Children.Add(tb[i]);
wp.Children.Add(cv[i]);
}

以上code是直线向前工程确定 - 直到我实现加,减,摧毁按钮在那里我可以

The above code is straight forwards works OK - Until I implement add, minus and destroy buttons where I could

1. Add an additional `Canvas` on a click event
2. Remove the last `Canvas` on a click event
3. Destroy a specific `Canvas` in the `WrapPanel` on a click event (may ba a little cross icon in each `Canvas`)

如果我处理上述3个动作的组合,我可以很容易地创建相同的索引的UI元素,或创建画布超出它已经初步申报的范围。

If I process some combination of the above 3 actions, I could easily create UIElements of the same index or create Canvas that goes out of the range of what it had been declared initially.

我看着列表但是,每个画布具有不同的属性(每个也有儿童的UIElement具有不同属性的),我想不出名单将如何解决这个问题。一个我去解决这个方法是声明一个超级大数组大小为画布(如画布[] CV =新的Canvas [99999] 但是我尽管这不是很有效。

I looked into List however, each Canvas have different properties (each also has UIElement Children with different properties) and I can't figure out how List would solve it. A way for me to go around that is to declare a super large Array size for Canvas (e.g. Canvas[] cv = new Canvas[99999] but I though that's not very efficient.

另外,如果我使用List,我怎么可能更改后的特定UIElement的特性,但它们产生?例如。如果我加10 Canvas和添加到列表,并全部产生后,他们,我需要选择5 Canvas和改变TextBox.Text,我怎么访问它就像我在一个数组做(即TB [5]。文本=你好)?

Also, if I use List, how could I change properties of a specific UIElement after the they are generated? E.g. If i add 10 Canvas and add to List, and after they are all generated, I need to select the 5th Canvas and change a TextBox.Text, how do I access it like I did in an Array (i.e. tb[5].Text = "Hello")?

谁能告诉我一些方法解决这个问题?

Can anyone show me some approaches to this problem?

推荐答案

只是一个直接翻译如何与一个列表,而不是下面做到这一点。鉴于你的code,我不知道你为什么要保持在一个列表中的帆布和textbox'es的轨迹 - 你可以访问儿童 WrapPanel 而不是直接 - 让我们假设你确实需要这些不同的藏品现在

Just a direct translation on how to do this with a list instead below. Given your code I don't know why you want to keep track of the canvas and textbox'es in a list - you can just access the children collection of the WrapPanel directly instead - let's assume you do need these separate collections for now.

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

 public void Init()
{

    int myInt = 500;
    // in a function (e.g. a Button_Click event) to generate the multiple Canvas in a WrapPanel
    for (int i = 0; i < myInt; i++)
    {
        Canvas cv = new Canvas();
        TextBox tb = new TextBox();
        cv.Children.Add(tb);
        wp.Children.Add(cv);

        cvList.Add(cv);
        tbList.Add(tb);
    }
}


public void AddCanvas()
{
    Canvas cv = new Canvas();
    TextBox tb = new TextBox();
    cv.Children.Add(tb);
    wp.Children.Add(cv);
    cvList.Add(cv);
    tbList.Add(tb);
}

public void RemoveCanvas()
{
        wp.Children.RemoveAt(wp.Children.Count-1);
        cvList.RemoveAt(cvList.Count - 1);
        tbList.RemoveAt(cvList.Count - 1);
}

修改作为补充意见:

例如。如果我加10帆布,并经过
  他们都是产生,我需要
  选择第5画布和更改
  TextBox.Text,我怎么访问它像
  我在做阵列(即TB [5]。文本=
  你好)?

E.g. If i add 10 Canvas, and after they are all generated, I need to select the 5th Canvas and change a TextBox.Text, how do I access it like I did in an Array (i.e. tb[5].Text = "Hello")?

您只需直接访问的孩子们。你知道你只加了画布元素的 WrapPanel 。所以,你可以做(​​WP是 WrapPanel 再次):

You can just access the children directly. You know you only added Canvas elements to your WrapPanel. So you could do (wp is the WrapPanel again):

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

这篇关于动态的和不可改变的UIElement阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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