Reputation:
i have my custom itemscontrol for displaying sudoku game grid. I want it to display its items to 9X9 grid. Each item has X and Y property and i want to bind to that property position in grid (Grid row and grid. column). Everything looks working except binding these grid.row and grid.column properties.. Code follows. It is not fault of binding because if i use hard values, nothing changes. Please help.:
<ItemsControl Margin="4" ItemsSource="{Binding Cells, Mode=OneWay}" x:Name="grid">
<ItemsControl.ItemTemplate>
<DataTemplate>
<grid:GridCell Grid.Column="{Binding X}" Grid.Row="{Binding Y}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid IsItemsHost="True" Background="Pink">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="2" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="2" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition Width="2" />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition Width="2" />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Edit: Nothing changes if i use for example
<ItemsControl.ItemTemplate>
<DataTemplate>
<grid:GridCell Grid.Column="2" Grid.Row="2" />
</DataTemplate>
</ItemsControl.ItemTemplate>
Upvotes: 2
Views: 4225
Reputation: 184516
The values are ignored because the cells are not the immediate children of the Grid
, they are wrapped in a ContentPresenter
created by the ItemContainerGenerator
of the ItemsControl
.
You need to apply the values at a higher level using the ItemContainerStyle
.
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Grid.Column" Value="{Binding X}" />
<Setter Property="Grid.Row" Value="{Binding Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
Upvotes: 9