【发布时间】:2011-04-08 13:27:27
【问题描述】:
这可能是个骗子,但我在搜索和阅读 MSDN 的一个小时左右内找不到任何东西。
我正在制作一个基于图块的 2D 游戏,并将 ItemsControl 绑定到一个图块列表(C# 类)。这很好用。
<ItemsControl ItemsSource="{Binding Tiles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding Board.Rows}" Columns="{Binding Board.Columns}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:Tile}">
<Grid x:Name="tileGrid" Background="{Binding TileImage}"/>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl>
每个图块都有一个内部 5x5 网格,因为我需要能够在每个图块的不同位置放置单元。每个Tile 都包含一个List<Location>,其中有一个单元,我想将其放置在该网格上。
<Grid x:Name="tileGrid" Background="{Binding TileImage}">
<ColumnDefinition snipped - there's 5 columns and 5 rows>
<ItemsControl ItemsSource="{Binding Locations}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type sys:Int32}">
<Image Source="unit.bmp"
Grid.Column="{Binding Converter={StaticResource ColumnConverter}}"
Grid.Row="{Binding Converter={StaticResource RowConverter}}"/>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
ColumnConverter 和 RowConverter 只是从 Location 中获取列索引。
问题是我无法从 ItemsControl 内部绑定到 tileGrid 的 Column 和 Row。有人建议将 Grid 放在 Locations ItemsControl 中,但这不起作用,因为它需要是外部 Tiles ItemsControl 的子级。
我能做什么?
【问题讨论】:
-
你指的是哪个网格作为外网格?统一网格?
-
我已经在问题中澄清了。我希望将位置集合定位在 5x5 tileGrid 内,但我不知道该怎么做。