Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF tiled background in pixels

Tags:

c#

wpf

I'm trying to tile the background of my canvas relative to pixel size. This is what i currently have...

enter image description here

I want it to look like this... I just want to create a tiled grid in the background using code. I don't want to use a png or image file to create the grid.

enter image description here

<Window x:Class="graph_view.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:graph_view"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Row="0" Text="10"></TextBox>
        <Canvas Grid.Row="1">
            <Canvas.Background>
                <DrawingBrush TileMode="Tile" Viewbox="0,0,20,20" ViewportUnits="RelativeToBoundingBox" AlignmentX="Left" AlignmentY="Top">
                    <DrawingBrush.Drawing>
                        <GeometryDrawing Brush="sc# 1.0, 0.02, 0.02, 0.02">
                            <GeometryDrawing.Geometry>
                                <GeometryGroup>
                                    <RectangleGeometry Rect="0,0,20,20" />
                                </GeometryGroup>
                            </GeometryDrawing.Geometry>
                            <GeometryDrawing.Pen>
                                <Pen Thickness="1" Brush="red"/>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Canvas.Background>
        </Canvas>
    </Grid>
</Window>
like image 629
JokerMartini Avatar asked Feb 28 '26 21:02

JokerMartini


1 Answers

<Canvas>
    <Canvas.Background>
        <VisualBrush TileMode="Tile" Stretch="Uniform" Viewport="10,10,10,10" ViewportUnits="Absolute">
            <VisualBrush.Visual>
                <Rectangle Width="10" Height="10" Fill="Black" Stroke="Red"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Canvas.Background>
</Canvas>

enter image description here

Change Rectangle StrokeThickness value to more thickness grid. Change ViewPort and Width and Height to more big grid.

<Canvas >
    <Canvas.Background>
        <VisualBrush TileMode="Tile" Stretch="Uniform" Viewport="20,20,20,20" ViewportUnits="Absolute">
            <VisualBrush.Visual>
                <Rectangle Width="20" Height="20" Fill="Black" Stroke="Red" StrokeThickness="0.1"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Canvas.Background>
</Canvas>

enter image description here

like image 141
ebattulga Avatar answered Mar 02 '26 12:03

ebattulga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!