ImageBrush for a BackGround grid in WPF

I've added a grid for my level editor and it was fairly simple once I understood how everything works together. I tried to use a DrawingBrush first, but it didn't work. When I reverted to an ImageBrush together with GeometryDrawing, it worked like a charm.

Here's the code for it. I'm drawing the lines on the edges of the canvas from (64,0) to (64,64) and (0,64) to (64,64).

        private void CreateBackGroundGrid()
        {
            int gridSize = 64;

            GeometryDrawing geometryDrawing = new GeometryDrawing();
            geometryDrawing.Pen = new Pen(new SolidColorBrush(Colors.DarkGray), 1);

            GeometryGroup group = new GeometryGroup();
            group.Children.Add(new LineGeometry(new Point(0, gridSize), new Point(gridSize, gridSize)));
            group.Children.Add(new LineGeometry(new Point(gridSize, 0), new Point(gridSize, gridSize)));
            geometryDrawing.Geometry = group;

            DrawingImage image = new DrawingImage(geometryDrawing);
            var brush = new ImageBrush(image);
            brush.Stretch = Stretch.None;
            brush.TileMode = TileMode.Tile;
            brush.Viewport = new Rect(0, 0, gridSize, gridSize);
            brush.ViewportUnits = BrushMappingMode.Absolute;

            Editor.Background = brush;
        }

Tags: WPF

maj 13 2011 3:19
Add a Comment

Blow him up!