When I swap my grid control for a stackpanel control the contents inside disappear. I could just use the grid control but not knowing bugs me :)
this works
<Grid> <views:ChessBoardView DataContext="{Binding Path=BoardViewModel}" ></views:ChessBoardView>
</Grid>
This doesn't:
<StackPanel>
<views:ChessBoardView DataContext="{Binding Path=BoardViewModel}" ></views:ChessBoardView>
</StackPanel>
Details:
View Model:
public class MainWindowViewModel : BaseViewModel
{
#region Properties
#region ChessBoardViewModel
private ChessBoardViewModel _BoardViewModel;
public ChessBoardViewModel BoardViewModel
{
get { return _BoardViewModel; }
set
{
if (_BoardViewModel != value)
{
_BoardViewModel = value;
RaisePropertyChanged(() => BoardViewModel);
}
}
}
#endregion
}
namespace Chess_Piece_Viewer.ViewModels
{
public class ChessBoardViewModel:BaseViewModel
{
public Color DarkSquareColor { get; set; }
public Color LightSquareColor { get; set; }
public ChessBoardViewModel()
{
DarkSquareColor = Colors.Black;
LightSquareColor = Colors.White;
}
}
Poor Mans Dependency Injection:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow = new Chess_Piece_Viewer.Views.MainWindow();
var MainViewModel = new Chess_Piece_Viewer.ViewModels.MainWindowViewModel();
MainViewModel.BoardViewModel = new ViewModels.ChessBoardViewModel();
MainWindow.DataContext = MainViewModel;
MainWindow.Show();
}
}
I've added the usercontrol that is disappearing
<UserControl x:Class="Chess_Piece_Viewer.Views.ChessBoardView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
</UserControl.Resources>
<UniformGrid Rows="8" Columns="8">
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
</Rectangle.Fill>
</Rectangle>
</UniformGrid>
</UserControl>
Thanks in advance for any help :)