下面是我的一個相關的帖子一些背景: How to databind using to a 2d array of objects of different types綁定列表<列表<MyClass>>細胞在網格
所以我有一個:
List<List<FarmyardSpace>>
我希望這可以表示爲按鈕在網格的單元格中。我不能像在相關文章中那樣使用UniformGrid,因爲我需要定義Grid中單元格的大小。理想情況下,我不想在我的數據中定義Row和Column字段(儘管也許這應該由ViewModel以某種方式處理?有一天剛開始學習這些東西,而且我仍然圍繞着WPF和MVVM) 。
這裏是我的最新嘗試,這是不行的(事實上拋出異常),並在這個特殊的例子中,我依靠現有的FarmyardSpaces列和行屬性:
<ItemsControl ItemsSource="{Binding FarmyardGrid}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding}">
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="15"/>
<RowDefinition />
<RowDefinition Height="15"/>
<RowDefinition />
<RowDefinition Height="15"/>
<RowDefinition />
<RowDefinition Height="15"/>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Row" Value="{Binding Row}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15"/>
<ColumnDefinition />
<ColumnDefinition Width="15"/>
<ColumnDefinition />
<ColumnDefinition Width="15"/>
<ColumnDefinition />
<ColumnDefinition Width="15"/>
<ColumnDefinition />
<ColumnDefinition Width="15"/>
<ColumnDefinition />
<ColumnDefinition Width="15"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding Column}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
我認爲部分這不起作用的原因是由外部ItemsControl包含的Items不是FarmyardSpaces而是List,所以它們沒有要綁定的Column屬性。我也嘗試了內部ItemsControl中的RowDefinitions和ColumnDefinitions。這擺脫了異常,但也不起作用,似乎對我來說是錯誤的,尤其是考慮到在外部ItemsControl中聲明Columns的相關帖子中使用UniformGrid時的解決方案。
無論如何,我會很感激任何幫助搞清楚這一點,在此先感謝!
好吧,我對於我現在沒有得到這個,感覺不那麼糟糕:)這個解決方案對我來說很有意義,但我認爲將會有一種處理索引的不太複雜的方式,因爲它似乎是想知道佈局的一個非常基本的事情目的。感謝所有的幫助! – StuartMorgan