public BoardDisplay() { // Initialize from XAML and get references to child elements _root = XamlReader.Load(Utility.GetXamlResource("SilverlightSudokuHelper.BoardDisplay.xaml")) as Canvas; _fadeStoryboard = _root.FindName("FadeStoryboard") as Storyboard; _fadeAnimation = _root.FindName("FadeAnimation") as DoubleAnimation; // Create the board frame _frame = new Rectangle(); _frame.Fill = new SolidColorBrush { Color = Colors.White }; _frame.Stroke = new SolidColorBrush { Color = Colors.Black }; _frame.StrokeThickness = 2; _frame.RadiusX = 5; _frame.RadiusY = 5; _root.Children.Add(_frame); // Create the board lines _verticalLines = new Line[Board.Size - 1]; _horizontalLines = new Line[Board.Size - 1]; for (var i = 0; i < _verticalLines.Length; i++) { bool majorLine = (0 == ((i + 1) % 3)); var verticalLine = new Line(); verticalLine.Stroke = new SolidColorBrush { Color = (majorLine ? Colors.Black : Colors.Gray) }; verticalLine.StrokeThickness = 2; verticalLine.SetValue(Canvas.ZIndexProperty, (majorLine ? 1 : 0)); _root.Children.Add(verticalLine); _verticalLines[i] = verticalLine; var horizontalLine = new Line(); horizontalLine.Stroke = new SolidColorBrush { Color = (majorLine ? Colors.Black : Colors.Gray) }; horizontalLine.StrokeThickness = 2; horizontalLine.SetValue(Canvas.ZIndexProperty, (majorLine ? 1 : 0)); _root.Children.Add(horizontalLine); _horizontalLines[i] = horizontalLine; } // Create the selection marker _marker = new Marker("SilverlightSudokuHelper.MarkerSelection.xaml"); _root.Children.Add(_marker); _markerPosition = new Point(4, 4); // Create the conflict marker _conflict = new Marker("SilverlightSudokuHelper.MarkerConflict.xaml"); _root.Children.Add(_conflict); // Create the value and candidate text blocks _values = new TextBlock[Board.Size, Board.Size]; _candidates = new TextBlock[Board.Size, Board.Size, 9]; for (var y = 0; y < Board.Size; y++) { for (var x = 0; x < Board.Size; x++) { var value = new TextBlock { FontFamily = FontFamily }; _root.Children.Add(value); _values[x, y] = value; for (var z = 0; z < 9; z++) { var candidate = new TextBlock { FontFamily = FontFamily, Foreground = new SolidColorBrush { Color = Colors.Gray } }; _root.Children.Add(candidate); _candidates[x, y, z] = candidate; } } } // Handle the Loaded event to do layout Loaded += new RoutedEventHandler(HandleLoaded); // Attach the constructed object to make it visible Content = _root; }
private void LayoutMarker(Marker marker, Point position) { // Update the marker's size and position marker.Visibility = Visibility.Visible; var cellRect = GetCellRect((int)position.X, (int)position.Y); marker.SetValue(Canvas.LeftProperty, cellRect.Left + MarkerMargin); marker.SetValue(Canvas.TopProperty, cellRect.Top + MarkerMargin); marker.Width = cellRect.Width - (2 * MarkerMargin); marker.Height = cellRect.Height - (2 * MarkerMargin); // Tell the marker to lay itself out marker.Layout(); }