private static void PrepareGrid(Frame frame, Grid parent)
{
var brush = new SolidColorBrush(_color);
_grid = new Grid();
_grid.IsHitTestVisible = false;
// To support both orientations, unfortunately more visuals need to
// be used. An alternate implementation would be to react to the
// orientation change event and re-draw/remove squares.
double width = frame.ActualWidth;
double height = frame.ActualHeight;
double max = Math.Max(width, height);
for (int x = 24; x < /*width*/ max; x += 37)
{
for (int y = 24; y < /*height*/ max; y += 37)
{
var rect = new Rectangle
{
Width = 25,
Height = 25,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(x, y, 0, 0),
IsHitTestVisible = false,
Fill = brush,
};
_grid.Children.Add(rect);
_squares.Add(rect);
}
}
_grid.Visibility = _visible ? Visibility.Visible : Visibility.Collapsed;
_grid.Opacity = _opacity;
// For performance reasons a single surface should ideally be used
// for the grid.
_grid.CacheMode = new BitmapCache();
// Places the grid into the visual tree. It is never removed once
// being added.
parent.Children.Add(_grid);
}