using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace firstWPF { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); const int BOARD_WIDTH = 300; const int BOARD_HEIGHT = 300; int side = BOARD_WIDTH; const int SQUARES_PER_SIDE = 8; int sideInPix = (int)(side / SQUARES_PER_SIDE); Brush lightBrush = Brushes.Bisque; Brush darkBrush = Brushes.ForestGreen; Brush currentBrush = lightBrush; ScaleTransform st = new ScaleTransform(((float)side) / SQUARES_PER_SIDE, ((float)side) / SQUARES_PER_SIDE); // Create an instance of the DrawingContext class DrawingContext dc = dv.RenderOpen(); dc.PushTransform(st); for (int row = 0; row < SQUARES_PER_SIDE; row++) { for (int column = 0; column < SQUARES_PER_SIDE; column++) { dc.DrawRectangle(currentBrush, new Pen(currentBrush, 0), new Rect(column, row, 1, 1)); currentBrush = (currentBrush.Equals(lightBrush)) ? darkBrush : lightBrush; } currentBrush = (currentBrush.Equals(lightBrush)) ? darkBrush : lightBrush; } dc.Close(); chessGroup.Children.Add(dv.Drawing); // Windows Forms uses "Immediate Mode" to // process a Paint Message. // WPF uses "Retained Mode" to process a // Paint Message. We never call Invalidate() // in WPF. boardButton.Click += BoardButton_Click; } private void BoardButton_Click(object sender, RoutedEventArgs e) { } private DrawingVisual dv = new DrawingVisual(); } }