WPF draw grid lines

Source: Internet
Author: User

The new software needs to draw a grid, and the shading like VISIO is easy to align when drawing. In WinForm, in the painting event, you can draw a line on the canvas through Graphics through computation. It will obviously be slow and there are many codes.

 

In WPF, it can be simpler. it is not convenient to draw a proper number of straight lines and improve performance. You can create a Group and add all the straight lines to the Group, then use RenderTransform or LayoutTransform to scale according to the scaling ratio. Another simpler method is to use ImageBrush. example:

 

Private DrawingBrush _ gridBrush;

Private void docCanvas_Loaded (object sender, RoutedEventArgs e)

{

If (_ gridBrush = null ){

_ GridBrush = new DrawingBrush (new GeometryDrawing (

New SolidColorBrush (Colors. White ),

New Pen (new SolidColorBrush (Colors. LightGray), 1.0 ),

New RectangleGeometry (new Rect (0, 0, 20, 20 ))));

_ GridBrush. Stretch = Stretch. None;

_ GridBrush. TileMode = TileMode. Tile;

_ GridBrush. Viewport = new Rect (0.0, 0.0, 20, 20 );

_ GridBrush. ViewportUnits = BrushMappingMode. Absolute;

DocCanvas. Background = _ gridBrush;

}

}

In fact, it is to use small rectangles to spell out the mesh of the background.

 

Author: nocky

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.