Biz*_*han 12 wpf grid templates itemscontrol
我正在尝试创建一个具有可变行数和列数的表.我有这样做ItemsControl
具有Grid
作为ItemsPanel
.我知道我可以设置Grid.Row
和Grid.Column
每个项目通过的ItemContainerStyle
.但是当我无法通过名称访问Grid时,我不知道如何更改行数和列数以及它们的大小.
你如何修改RowDefinitions
或ColumnDefinitions
的Grid
使用只在运行时XAML和结合与无代码隐藏?
这是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>
Run Code Online (Sandbox Code Playgroud)
我试图添加一些RowDefinition
代码,但我找不到myGrid
通过其名称(或任何其他方式)访问的方法,因为它在一个内部ItemsPanelTemplate
.
我想知道是否有任何方法可以在运行时以编程方式添加或修改RowDefinitions
?
Rac*_*hel 28
您可以使用附加属性来Grid
修改,RowDefinitions
以及ColumnDefinitions
何时设置或更改这些属性.
它可以让你这样写Grid
:
<Grid local:GridHelpers.RowCount="{Binding MaxGridRow}"
local:GridHelpers.ColumnCount="3" />
Run Code Online (Sandbox Code Playgroud)
然后只显示一个属性ViewModel
,返回Cells
集合中最大的行号.
您可以在我的博客上找到这些属性的详细实现.
Y.Y*_*hus 10
如果您可以从代码隐藏中访问网格,则可以执行以下操作:
var rowDefinition = new RowDefinition();
rowDefinition.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowDefinition);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26894 次 |
最近记录: |