简体   繁体   English

我该如何在WPF中表示这些记录和字段?

[英]How should I represent these records and fields in WPF?

I'm fairly new to C#. 我对C#很新。 How do you work with multidimensional arrays? 你如何使用多维数组? I am also using WPF. 我也在使用WPF。

To be more clear on my problem, here is what I am trying to do: 为了更清楚我的问题,这是我想要做的:

I have an array that needs to store about 200 'records' and each record has three fields, which the user has entered using a textbox. 我有一个数组需要存储大约200个'记录',每个记录有三个字段,用户使用文本框输入。 So I think this is how you would set the array up: 所以我认为这就是你如何设置阵列:

 string[,] Properties = new string[500, 3];

I am going to add a new 'record' to that array every time the user hits a button. 每次用户点击按钮时,我都会向该数组添加一个新的“记录”。 I am clueless as to how you would do that. 我对你如何做到这一点毫无头绪。 I am sure that I am going to need to set up some sort of counter. 我确信我需要设置一些计数器。

I also am going to have to update and delete values in the array. 我还必须更新和删除数组中的值。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank-You. 谢谢。

I'll sum up what the others have suggested, because you're new at this. 我会总结其他人的建议,因为你是新手。

Create a class for your record (this example is rather simplistic): 为您的记录创建一个类(这个例子相当简单):

class MyRecord
{
    public string Property1 {get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
}

Then create a List of those records: 然后创建这些记录的列表:

List<MyRecord> theList;

Use theList.Add to add a new record to the list. 使用theList.Add将新记录添加到列表中。

You cannot easily add to an array. 您无法轻松添加到阵列。 The only option you do have is to allocate a new array with another row, copy all the old items and put the new data in the last row. 您唯一的选择是分配一个新数组与另一行,复制所有旧项目并将新数据放在最后一行。

Likewise for deleting a row: You'd have to move all items after the deleted row “up” and make the array shorter. 同样,对于删除行:您必须在删除的行“up”之后移动所有项目并使数组缩短。 Which boils down to allocating a new array and copying all items. 归结为分配新阵列和复制所有项目。 See above :-) 往上看 :-)

Generally arrays are a low-level construct and should rarely be used by developers in C#. 通常,数组是一个低级构造,很少被C#中的开发人员使用。 Especially if you want to model a mutable collection. 特别是如果你想建模一个可变的集合。 If you want a mutable collection of things, then use a collection of things, namely List<Thing> for example. 如果你想要一个可变的东西集合,那么使用一组东西,例如List<Thing> Thing would be a class containing three properties that make up your records. Thing将是一个包含构成记录的三个属性的类。

The collection classes handle all the common cases like inserting and adding items, removing items at arbitrary positions, etc. Since you mentioned WPF, they also play nicely with the data binding and templating capabilities of WPF which are very powerful tools for displaying data in a UI. 集合类处理所有常见情况,如插入和添加项目,删除任意位置的项目等。由于您提到了WPF,它们也很好地与WPF的数据绑定和模板功能配合使用,WPF是用于在数据中显示数据的非常强大的工具。 UI。

You should probably use a list instead 您可能应该使用列表

LinkedList<String[]> properties = new LinkedList<String[]>();

and then you can add/delete entries 然后你可以添加/删除条目

properties.AddLast({"a","b","c"});

the advantage of using a list over an array is that deleting elements from arbitrary positions is easy :) 在数组上使用列表的优点是从任意位置删除元素很容易:)

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

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