Search code examples
wpfxamlbackgroundbrushdiagonal

How do I draw two-colored diagonal background?


How do I draw two-colored diagonal background? I want colors to be separated by diagonal of container. I want diagonal to be preserved after resizing of container.

I have two categories of rows in my DataGrid dustinguished by colors. Few rows has both categories so I want to set their background like above.


Solution

  • This can be achieved by using a DrawingBrush. Here is an example for the background of a Canvas, but it can be used for the background of anything.

        <Canvas Margin="182,229,197,43">
            <Canvas.Background>
                <DrawingBrush>
                    <DrawingBrush.Drawing>
                        <DrawingGroup>
                            <DrawingGroup.Children>
                                <GeometryDrawing Brush="Aquamarine" Geometry="M 1,1 L 0,1 0,0 1,1"/>
                                <GeometryDrawing Brush="Blue" Geometry="M 0,0 L 1,1 1,0 0,0"/>
                            </DrawingGroup.Children>
                        </DrawingGroup>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Canvas.Background>
        </Canvas>
    

    Resizing is not a problem as you can see in the attached pictures. enter image description here