Simple Path Data Resources that I Add to Every WPF and Silverlight Project
UPDATE #1: someone asked for the rest of the ResourceDictionary. It’s very basic, but here it is if you’re interested. Basically, it just contains sections for the various types of resources you might create.
Here’s a little time saver. I sort of have a routine that I go through when I create a new WPF project. One of those things is to create a resource dictionary (I’m down to one on most projects now, but more on that later) that includes some common stuff that is nice to be able to depend on while your cranking out XAML.
Among those, are these super simple geometry resources:
<Geometry x:Key="DownArrow">M0,0 L1,0 0.5,1Z</Geometry> <Geometry x:Key="UpArrow">M0,1 L1,1 0.5,0Z</Geometry> <Geometry x:Key="RightArrow">M0,0 L1,0.5 0,1Z</Geometry> <Geometry x:Key="LeftArrow">M0,0.5 L1,1 1,0Z</Geometry> <Geometry x:Key="CloseX">M0,0 L1,1 M0,1 L1,0</Geometry>
Simple, but handy when you (inevitably) need to bust out an arrow for some reason. You use them like this:
<!-- DownArrow --> <Path Data="{StaticResource DownArrow}" Width="10" Height="8" Stretch="Fill" Fill="Black" /> <!-- CloseX --> <Path Data="{StaticResource CloseX}" Width="12" Height="12" Stretch="Fill" Stroke="Black" StrokeThickness="3" Margin="10" />
All together, they look like this:![]()
The geometries themselves all happen within a real coordinate space of a pixel, so it’s important to set the Stretch property to Fill, that way the geometry with stretch to the size of the Path element.
Unfortunately, this is WPF only. Silverlight (SL2 at least) doesn’t like to work with Geometries as resources. If someone tries it out in SL3, let me know how it goes.
UPDATE #2: Dr. WPF (ironically) suggested a sweet workaround for Silverlight. Instead of adding the resources as Geometries, add them as strings, like this:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Grid.Resources> <sys:String x:Key="DownArrow">M0,0 L1,0 0.5,1Z</sys:String> <sys:String x:Key="UpArrow">M0,1 L1,1 0.5,0Z</sys:String> <sys:String x:Key="RightArrow">M0,0 L1,0.5 0,1Z</sys:String> <sys:String x:Key="LeftArrow">M0,0.5 L1,1 1,0Z</sys:String> <sys:String x:Key="CloseX">M0,0 L1,1 M0,1 L1,0</sys:String> </Grid.Resources> </Grid>
The usage from XAML is then exactly the same as WPF. Nice one, doc!
Dr. WPF
04 jun 2009
Nope. Still doesn’t work in SL3. Lame!
What does work in SL3 is to declare the path data using strings:
M0,0 L1,0 0.5,1Z
M0,1 L1,1 0.5,0Z
M0,0 L1,0.5 0,1Z
M0,0.5 L1,1 1,0Z
M0,0 L1,1 M0,1 L1,0
where the sys namespace is declared as xmlns:sys=”clr-namespace:System;assembly=mscorlib”.
The usage from a Path element is exactly the same:
Note that this string approach will not work in WPF (and rightly so… it’s a hack), so file it under nonportable markup.
Cheers!
-dw
Dr. WPF
04 jun 2009
Looks like your blog didn’t care for the markup tags in the above comment. Good luck fixing that!
Simple Path Data Resources that I Add to Every WPF Project | Silverlight Coder
04 jun 2009
[...] rest is here: Simple Path Data Resources that I Add to Every WPF Project Share and [...]
Stefan Olson
04 jun 2009
Dr. WPF,
I’ve filed a bug regarding the inability to share paths in Silverlight:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=425338
Hopefully they’ll get it fixed in Silverlight 4, then you might be able to become Dr. Silverlight as well!
…Stefan
Stefan Olson
04 jun 2009
I should’ve made it clear that the situation I’m referring to is where you want to reuse the path, rather than the geometry.
…Stefan
Robby Ingebretsen
17 jun 2009
@Dr. WPF — sweet workaround for Silverlight! I’ll update the post!