我试图创建一个可变行数和列数的表格。我使用了一个带有
我想知道是否有办法在运行时通过编程方式添加或修改
Grid
作为其ItemsPanel
的ItemsControl
。我知道可以通过其ItemContainerStyle
设置每个项的Grid.Row
和Grid.Column
。但是当我无法通过名称访问Grid
时,我不知道如何更改行数、列数及其大小。
问题:
如何使用绑定在运行时修改Grid
的RowDefinitions
或ColumnDefinitions
,并且不需要任何代码后台?
以下是XAML代码:
<ItemsControl Name="myItemsControl" ItemsSource="{Binding Cells}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid Name="myGrid">
<Grid.RowDefinitions>
<!-- unknown number of rows are added here in run-time -->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<!-- known number of columns are added here in run-time -->
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style.../>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
我试图在代码后端添加一些RowDefinition
,但我找不到以其名称(或其他任何方式)访问myGrid
的方法,因为它位于ItemsPanelTemplate
内。我想知道是否有办法在运行时通过编程方式添加或修改
RowDefinitions
?
.ToString()
是因为星形列/行以前是整数,后来有一天我需要多个星形列,决定将其更新为字符串。 - Rachel<Window>
标签中会出现类似于xmlns:local="clr-namespace:MyProjectNamespace"
的内容。它告诉WPF使用术语“local”作为指向XML命名空间MyProjectNamespace
的快捷方式。 - Rachel