Tip

Create graph lines with GeometryBrush

If you create an application where the user can drag content on the screen you should consider enabling alignment features. Most graphics tools follow this approach. For example, in Visual Studio you get snaplines, which appear when the edge of the dragged control is aligned with other elements in the designer. Another common UI metaphor is drawing a pattern of lines on the screen. I liked to turn these on when user Expression Blend I find them helpful when positioning my graphic assets.

In this tip, I'll show you how to use the GeomrtryBrush to draw graph lines.

Example in Expression Blend
In Blend I click the 'Show snap grid' button on the bottom of the Artboard.

Expression Blend overlays a light gray grid of lines.

Creating the WPF GeometryBrush
The key to mimicking the Expression Blend snap grid is to create a GeometryBrush.

This example defines a brush in the root grid. In the DrawingGroup I create two lines perpendicular to each other. Then in the DrawingBrush I tell WPF to Tile (repeat) my brush repeatedly across the screen. The Viewport property is where I set the dimension of the tile.

Now I layer another Grid over the first and paint its Background with my DrawingBrush.

Here are the results.

By altering the viewport I can control the size of the tile. Here is what the Grid looks like when I set the Viewport="0,0,24,36".

Using the line brush on other shapes
Naturally I can use the brush on other WPF elements. Here is an example using an Ellipse.


This was first published in August 2010

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.