动态创建和命名网格元素

时间:2022-09-27 21:22:16

I have been trying to create a Gantt chart style timetable for a project I am working on. I am currently having trouble with the UI of said chart.

我一直在尝试为我正在开发的项目创建一个甘特图样式时间表。我目前在使用所述图表的UI时遇到问题。

动态创建和命名网格元素

As you can see from my highly advanced mockup, events will be represented by a box (a grid) stretching from start to end time.

从我的高级模型中可以看出,事件将由从开始到结束时间延伸的框(网格)表示。

The problem I am having is creating these grids an infinite number of times from my code. I would like, ideally, for each grid to have it's own unique but dynamically generated name. For example, the "Breakfast" event's grid would be called grid1, the Internationals Breakfast would be called grid2, the Keys Workshop grid3, as so on for as many grids as my program creates, potentially to infinity.

我遇到的问题是从我的代码中无限次地创建这些网格。理想情况下,我希望每个网格都拥有自己独特但动态生成的名称。例如,“Breakfast”事件的网格将被称为grid1,Internationals Breakfast将被称为grid2,Keys Workshop grid3,就像我的程序创建的网格一样多,可能是无限的。

So, I know how to create grids with

所以,我知道如何创建网格

 Grid aGrid = new Grid();

But how would I go about giving each grid a different name? I hope I've been vaguely coherent...

但是,我如何为每个网格赋予不同的名称?我希望我隐约有点连贯......

1 个解决方案

#1


0  

do you mean a different variable name?

你的意思是一个不同的变量名称?

if you have a list of grids, you should be storing them in some type of collection, like a List<Grid> or ObservableCollection<Grid>, then you dont need to worry about naming them.

如果你有一个网格列表,你应该将它们存储在某种类型的集合中,比如List 或ObservableCollection ,那么你不必担心命名它们。

if thats not what you meant, and you want a unique Element Name for each Grid, then you could just have a grid counter that increments for each grid you create, and use that as the grid name

如果这不是你的意思,并且你想为每个网格一个唯一的元素名称,那么你可以只为你创建的每个网格增加一个网格计数器,并将其用作网格名称

Grid aGrid = new Grid();
aGrid.Name = "Grid" + gridCounter++;

#1


0  

do you mean a different variable name?

你的意思是一个不同的变量名称?

if you have a list of grids, you should be storing them in some type of collection, like a List<Grid> or ObservableCollection<Grid>, then you dont need to worry about naming them.

如果你有一个网格列表,你应该将它们存储在某种类型的集合中,比如List 或ObservableCollection ,那么你不必担心命名它们。

if thats not what you meant, and you want a unique Element Name for each Grid, then you could just have a grid counter that increments for each grid you create, and use that as the grid name

如果这不是你的意思,并且你想为每个网格一个唯一的元素名称,那么你可以只为你创建的每个网格增加一个网格计数器,并将其用作网格名称

Grid aGrid = new Grid();
aGrid.Name = "Grid" + gridCounter++;