グリッド コントロールをスタックパネル コントロールに交換すると、内部のコンテンツが消えます。私はグリッドコントロールを使用することができましたが、バグを知らなかった:)
これは動作します
<Grid> <views:ChessBoardView DataContext="{Binding Path=BoardViewModel}" ></views:ChessBoardView>
</Grid>
これはしません:
<StackPanel>
<views:ChessBoardView DataContext="{Binding Path=BoardViewModel}" ></views:ChessBoardView>
</StackPanel>
詳細:
モデルを見る:
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;
}
}
貧乏人の依存性注入:
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();
}
}
消えているユーザーコントロールを追加しました
<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>
助けてくれてありがとう:)