我正在使用网格(Grid)作为一个列表(ItemsControl)的项(ItemPanel), 并通过数据绑定进行动态绑定。以下代码可以工作 - 但是还存在一个问题: 我找不到一种方法来动态初始化网格(Grid)的列(ColumnDefinitions)和行(RowDefinitions). 因此,所有值都被放置在彼此之上。
<ItemsControl ItemsSource="{Binding Cells}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding RowIndex}"/>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
请注意,我正在根据MVVM模式搜索答案。因此,子类化和代码后置仅是解决方法,而不是解决方案。