35
I am creating a WPF form. One of the requirements is that it have a sector-based layout so that a control can be explicitly placed in one of the sectors/cells.
I have created a tic-tac-toe example below to convey my problem:
There are two types and one base type:
public class XMoveViewModel : MoveViewModel
{
}
public class OMoveViewModel : MoveViewModel
{
}
public class MoveViewModel
{
public int Row { get; set; }
public int Column { get; set; }
}
The DataContext of the form is set to an instance of:
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
Moves = new ObservableCollection<MoveViewModel>()
{
new XMoveViewModel() { Row = 0, Column = 0 },
new OMoveViewModel() { Row = 1, Column = 0 },
new XMoveViewModel() { Row = 1, Column = 1 },
new OMoveViewModel() { Row = 0, Column = 2 },
new XMoveViewModel() { Row = 2, Column = 2}
};
}
public ObservableCollection<MoveViewModel> Moves
{
get;
set;
}
}
And finally, the XAML looks like this:
<Window.Resources>
<DataTemplate DataType="{x:Type vm:XMoveViewModel}">
<Image Source="XMove.png" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Column}" Stretch="None" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:OMoveViewModel}">
<Image Source="OMove.png" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Column}" Stretch="None" />
</DataTemplate>
</Window.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding Path=Moves}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition />