Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Itemscontrols data binding to the same Grid
    text
    copied!<p>This may be a dupe, but I couldn't find anything in an hour or so of searching and reading MSDN.</p> <p>I'm making a 2D tile-based game and have an ItemsControl bound to a list of Tiles (C# class). This works great.</p> <pre><code>&lt;ItemsControl ItemsSource="{Binding Tiles}"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;UniformGrid Rows="{Binding Board.Rows}" Columns="{Binding Board.Columns}"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.Resources&gt; &lt;DataTemplate DataType="{x:Type local:Tile}"&gt; &lt;Grid x:Name="tileGrid" Background="{Binding TileImage}"/&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.Resources&gt; &lt;ItemsControl&gt; </code></pre> <p>There's an inner 5x5 Grid for each Tile since I need to be able to place units at various locations on each Tile. Each <code>Tile</code> contains a <code>List&lt;Location&gt;</code> where there is a unit, which I want to place on that Grid.</p> <pre><code>&lt;Grid x:Name="tileGrid" Background="{Binding TileImage}"&gt; &lt;ColumnDefinition snipped - there's 5 columns and 5 rows&gt; &lt;ItemsControl ItemsSource="{Binding Locations}"&gt; &lt;ItemsControl.Resources&gt; &lt;DataTemplate DataType="{x:Type sys:Int32}"&gt; &lt;Image Source="unit.bmp" Grid.Column="{Binding Converter={StaticResource ColumnConverter}}" Grid.Row="{Binding Converter={StaticResource RowConverter}}"/&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.Resources&gt; &lt;/ItemsControl&gt; </code></pre> <p>Where ColumnConverter and RowConverter just get the column index from a Location.</p> <p>The problem is that I can't bind to tileGrid's Column and Row from inside the ItemsControl. Some people have suggested putting the Grid inside the Locations ItemsControl, but that doesn't work because it needs to be a child of the outer Tiles ItemsControl.</p> <p>What can I do?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload