[英]How to update a grid in a listbox with more rows and columns dynamically and programmatically in code (C# and WPF)
I have a ListBox where every of its item has a DataTemplate to define its structure. 我有一个ListBox,其中每个项目都有一个DataTemplate来定义其结构。 It initially has a Grid that contains 6 rows and 4 columns.
它最初具有一个包含6行4列的网格。 But then during runtime I want to add to a specific ListBoxItem more rows and columns to that Grid in that DataTemplate.
但是然后在运行时,我想向该DataTemplate中的该网格添加更多行和列到特定的ListBoxItem。 How do I do that?
我怎么做? Do I need to create a new DataTemplate that contains the Grid with added rows and columns?
我是否需要创建一个新的DataTemplate,其中包含带有添加的行和列的网格? I have seen so much examples that looks like what I want to achieve that got confused right now with all the Bindings, ObservableCollections, ViewModel and so on.
我看到了太多的示例,这些示例看起来像我想要实现的目标,这些示例现在与所有Bindings,ObservableCollections,ViewModel等混淆了。 What should I do?
我该怎么办? Here is the WPF code below for that specific ListBox:
以下是该特定ListBox的WPF代码:
<ListBox Margin="0,138,28.58,156" Name="list" HorizontalAlignment="Right" Width="387.235" SelectionChanged="list_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Actually there is another op asking similar question. 实际上,还有另一个操作员问类似的问题。 You could take a look at his question.
您可以看一下他的问题。 His question is actually your answer.
他的问题实际上是您的答案。
He uses a custom control that derivates from Grid and he calls it Board. 他使用了一个派生自Grid的自定义控件,并将其称为Board。 Futhermore he adds rows and columns at run-time which seems exactly what you would like to achive.
此外,他在运行时添加了行和列,这似乎正是您想要实现的。
Here is the link: wpf MouseMove-Event in Board class does not work 这里是链接: Board类中的wpf MouseMove-Event不起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.