長方形オブジェクト内のデータバインディングが失敗しています。データコンテキストが渡されていないと思われます。DarkSquareColor の高レベル トレースを次に示します。
System.Windows.Data Warning: 56 : Created BindingExpression (hash=15409413) for Binding
(hash=9290279)
System.Windows.Data Warning: 58 : Path: 'DarkSquareColor'
System.Windows.Data Warning: 60 : BindingExpression (hash=15409413): Default mode resolved to
OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=15409413): Default update trigger
resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=15409413): Attach to
System.Windows.Media.SolidColorBrush.Color (hash=62178992)
System.Windows.Data Warning: 64 : BindingExpression (hash=15409413): Use Framework mentor <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=15409413): Resolving source
System.Windows.Data Warning: 69 : BindingExpression (hash=15409413): Framework mentor not found
System.Windows.Data Warning: 65 : BindingExpression (hash=15409413): Resolve source deferred
System.Windows.Data Warning: 95 : BindingExpression (hash=15409413): Got
InheritanceContextChanged event from SolidColorBrush (hash=62178992)
System.Windows.Data Warning: 67 : BindingExpression (hash=15409413): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=15409413): Found data context element:
Rectangle (hash=59316889) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=15409413): DataContext is null
'Chess Piece Viewer.vshost.exe' (Managed (v4.0.30319)):
Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-
SystemXmlLinq\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXmlLinq.dll'
Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-
SystemXml\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXml.dll'
System.Windows.Data Warning: 67 : BindingExpression (hash=15409413): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=15409413): Found data context element:
Rectangle (hash=59316889) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=15409413): DataContext is null
Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-
SystemData\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemData.dll'
失敗した xaml の一部:
<Border BorderBrush="{Binding Path=ChessBoard.BoardBorderBrush}" BorderThickness="{Binding Path=ChessBoard.BoardBorderThickness}" >
<UniformGrid Rows="8" Columns="8" >
<Canvas>
<Rectangle x:Name="a1" Stroke="{Binding Path=ChessBoard.DarkSquareBorder}">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=ChessBoard.DarkSquareColor, diagnostics:PresentationTraceSources.TraceLevel=High}"/>
</Rectangle.Fill>
</Rectangle>
<ContentControl DataContext="{Binding Path=ChessSquareViewModels[0]}"/>
</Canvas>
</UniformGrid>
</Border>
</UserControl>
ビューモデル:
namespace Chess_Piece_Viewer.ViewModels
{
class ChessGameViewModel :BaseViewModel
{
#region BackingFields
private IChessBoard _ChessBoard;
private IChessGame _ChessGame;
private DataTemplateSelector _ChessDataTemplateSelector;
#endregion
#region PublicFields
public ChessSquareViewModel[] ChessSquareViewModels = new ChessSquareViewModel[64];
#endregion
#region Properties
public DataTemplateSelector ChessDataTemplateSelector
{
get
{
return _ChessDataTemplateSelector;
}
set
{
if (_ChessDataTemplateSelector != value)
{
_ChessDataTemplateSelector = value;
RaisePropertyChanged(() => ChessDataTemplateSelector);
}
}
}
public IChessBoard ChessBoard
{
get
{
return _ChessBoard;
}
set
{
if (_ChessBoard != value)
{
_ChessBoard = value;
RaisePropertyChanged(() => ChessBoard);
}
}
}
public IChessGame ChessGame
{
get
{
return _ChessGame;
}
set
{
if (_ChessGame != value)
{
_ChessGame = value;
RaisePropertyChanged(() => ChessGame);
}
}
}
public ChessPieces[] CurrentPosition { get; set; }
public int MoveIndex { get; set; }
#endregion
#region CTOR
public ChessGameViewModel()
{
}
public ChessGameViewModel(IChessGame chessgame, ChessPieceDataTemplateSelector chesspiecedatatemplateselector)
{
this.ChessGame = chessgame;
this.ChessDataTemplateSelector = chesspiecedatatemplateselector;
this.ChessBoard = new ChessBoard();
//initialize chesssquareviewmodels?
for (int i = 0; i < 64; i++)
{
ChessSquareViewModels[i] = new ChessSquareViewModel();
}
MoveIndex = 0;
//initialize moves?
Positions = ChessGame.ChessPositionsasArray;
CurrentPosition=Positions[MoveIndex];
UpdateVisualChessSet();
UpdateVisualChessPosition();
}
#endregion
}
}
モデル:
namespace Chess_Piece_Viewer.Models
{
class ChessBoard:IChessBoard
{
private Color _DarkSquareColor;
public System.Windows.Media.Color DarkSquareColor {
get{
return _DarkSquareColor;
}
set {
_DarkSquareColor = value;
}
}
public System.Windows.Media.Color LightSquareColor { get; set; }
public System.Windows.Media.Brush DarkSquareBrush { get; set; }
public System.Windows.Media.Brush LightSquareBrush { get; set; }
public System.Windows.Media.Brush BoardBorderBrush { get; set; }
public double BoardBorderThickness { get; set; }
#region CTOR
public ChessBoard()
{
DarkSquareColor = Colors.Black;
LightSquareColor=Colors.White;
DarkSquareBrush = new SolidColorBrush(Colors.Black);
LightSquareBrush = new SolidColorBrush(Colors.White);
BoardBorderBrush = new SolidColorBrush(Colors.Black);
BoardBorderThickness = 2;
}
#endregion
}
}
貧乏人の依存性注入:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow = new Chess_Piece_Viewer.MainWindow();
// IChessSet chessset = (IChessSet) MainWindow.FindResource("Chaturanga Chess Set");
ChessPieceDataTemplateSelector dts = new ChessPieceDataTemplateSelector();
var examplechessgame = new Chess_Piece_Viewer.Models.DesignTimeData.ExampleChessGame();
var MainViewModel= new Chess_Piece_Viewer.ViewModels.ChessGameViewModel(examplechessgame, dts);
//var MainViewModel = new Chess_Piece_Viewer.ViewModels.MainWindowViewModel();
//MainViewModel.BoardViewModel = new ViewModels.ChessBoardViewModel();
MainWindow.DataContext = MainViewModel;
MainWindow.Show();
}
}