Fit XAML path into view - wpf

I have the following path. The data is from some data file:
<Path StrokeThickness="5" Stroke="Black" Fill="Yellow" Data="M 30330.8847248349,-37724.909002528 L 28556.3829935257,-37596.5557453925 28031.7660050946,-38008.0608378072 27746.4689258268,-38895.6687979239 27655.7148993139,-39397.1764657993 27718.5888966473,-39755.4955438608 27628.0246979519,-40621.440862981 28514.7500540091,-41208.8847446069 29093.8320242864,-40459.5872073251 29768.3831435369,-40107.7166927063 30092.4525793664,-39483.6045857995 30784.2658266352,-38627.7070622611 30811.846313938,-38537.1499823241 30358.6906348136,-37734.2759127391 30330.8847248349,-37724.909002528" />
I like to know what this looks like, so I want to render it in Kaxaml or XamlPad. Is there an easy way to resize this path so it will render nicely centered on the screen?

Just set the path's stretch attribute to Uniform to maitain the apsect ratio and you should be able to see it in KaXaml
<Path Stretch="Uniform" StrokeThickness="5" Stroke="Black" Fill="Yellow" Data="M 30330.8847248349,-37724.909002528 L 28556.3829935257,-37596.5557453925 28031.7660050946,-38008.0608378072 27746.4689258268,-38895.6687979239 27655.7148993139,-39397.1764657993 27718.5888966473,-39755.4955438608 27628.0246979519,-40621.440862981 28514.7500540091,-41208.8847446069 29093.8320242864,-40459.5872073251 29768.3831435369,-40107.7166927063 30092.4525793664,-39483.6045857995 30784.2658266352,-38627.7070622611 30811.846313938,-38537.1499823241 30358.6906348136,-37734.2759127391 30330.8847248349,-37724.909002528" />

Related

WPF change icon color created using canvas and path

I have an icon created with canvas and path. I can reuse this icon, but how can I change the color of the icon from "outside" (i.e. from view)
<Canvas x:Key="MyIcon" Width="40.000" Height="23.889">
<!-- Layer 1/<Group>/<Compound Path> -->
<Path Fill="#ffffffff" Data="F1 M 36.074,14.516 C 36.117,10.711 33.078,7.540 29.332,7.478 Z"/>
<!-- Layer 1/<Group>/<Compound Path> -->
<Path Fill="#ffffffff" Data="F1 M 35.848,2.368 C 36.766,3.651 37.097,3.832 38.095,3.558 C 38.345,3.489 38.523,3.159 38.735,2.950 C 38.499,2.756 38.281,2.426 38.025,2.395 Z"/>
<!-- Layer 1/<Group>/<Path> -->
<Path Fill="#ffffffff" Data="F1 M 35.307,14.487 C 35.268,17.908 32.499,20.618 29.098,20.563 C 25.615,20.506 22.916,17.747 22.947,14.274 Z"/>
</Canvas>
With this icon, it will always be white. What changes I can make so I will be able to change the color from xaml? For example now I show the icon like this:
<ContentControl Content="{StaticResource MyIcon}" />
I'm assuming to change the color the syntax will look something like this:
<ContentControl Content="{StaticResource MyIcon}" Color={StaticResource BlueBrush} />
Turn the Canvas into a Geometry resource by concatening the Data strings of the three Paths:
<Geometry x:Key="MyIcon">F1 M 36.074,14.516 C 36.117,10.711 33.078,7.540 29.332,7.478 Z M 35.848,2.368 C 36.766,3.651 37.097,3.832 38.095,3.558 C 38.345,3.489 38.523,3.159 38.735,2.950 C 38.499,2.756 38.281,2.426 38.025,2.395 Z M 35.307,14.487 C 35.268,17.908 32.499,20.618 29.098,20.563 C 25.615,20.506 22.916,17.747 22.947,14.274 Z</Geometry>
The use that resource with a single Path element instead of a ContentControl:
<Path Data="{StaticResource MyIcon}" Fill="Blue"/>

Re-sizing WPF Geometry Path

I'm working on a WPF application. Given a geometry string path, such as:
F1 M 27,18L 23,26L 33,30L 24,38L 33,46L 23,50L 27,58L 45,58L 55,38L 45,18L 27,18 Z
Is it possible to scale the drawing to a width and height (no matter how small/large the original was) while keeping the figure as a whole, and then finally return the string path representation of the new scaled figure?
There is no need to scale the values in a path geometry string. Just put it in the Data property of a Path control and set its Width, Height and Stretch properties as needed:
<Path Data="F1 M27,18 L23,26 33,30 24,38 33,46 23,50 27,58 45,58 55,38 45,18 27,18 Z"
Width="100" Height="100" Stretch="Uniform" Fill="Black"/>
Yes you can do it! The only thing you need to do, is to use a Viewbox for wrapping the item. This is a sample with a code that I had done, in this case I used the geometry as a DrawingBrush
...
<UserControl.Resources>
<DrawingBrush x:Key="Field" Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="{StaticResource FieldGrassBrush}" Geometry="F1 M 91.733,119.946C 92.8352,122.738 93.9374,125.529 92.9241,129.209C 91.9107,132.889 88.7819,137.458 84.4263,139.271C 80.0707,141.084 74.4885,140.142 70.8885,137.982C 67.2885,135.822 65.6707,132.444 65.1819,129.182C 64.693,125.92 65.333,122.773 65.973,119.626L 0.16,53.9203C 0.444319,53.4758 0.728638,53.0312 3.48413,48.7023C 6.23962,44.3733 11.4663,36.16 18.5596,28C 25.653,19.84 34.613,11.7333
........
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</UserControl.Resources>
...
Then the View Box (Note that the Grid has fixed Height and Width, but it will be stretched to the Viewbox's size, in this case, in an uniform way):
...
<Viewbox Stretch="Uniform" Grid.Row="2" Grid.ColumnSpan="2">
<Grid Height="300" Width="390">
<Rectangle Fill="{DynamicResource Field}" StrokeThickness="0"/>
...

WPF Show part of drawing path

I have a drawing with <Path> element. It contains dificult Data attribute so how I can show part of this path.
There are certainly several ways to do this. A very easy one would be to put the Path in a Canvas, set its position inside the Canvas and clip it by the Canvas' bounds:
<Canvas Width="80" Height="60" ClipToBounds="True">
<Path Canvas.Left="-10" Canvas.Top="-10" Data="M0,0 L100,0 100,100 Z"
Stroke="Black" StrokeThickness="2" Fill="AliceBlue"/>
</Canvas>
make use of Blend tool for this where if you copy the data attribute for the path, you could see how it is generated.
It would make your job easier to modify the path as well see the data generation at the same time.

What is the Data value of plus sign in Path

How to draw plus and minus signs in using data property in Path object.
This is my triangle path object. I need to change it to plus symbol.
<Path x:Name="trianglePath" Data="M 0 8 H 12 V 15 Z"/>
Here, I've created a plus and minus sign which is 10 x 10 pixels using XAML path markup syntax:
<Path Margin="10" Stroke="White" Data="M0,5 H10 M5,5 V10Z" StrokeThickness="2" Height="10" Width="10" />
<Path Margin="10" Stroke="White" Data="M0,5 H10" StrokeThickness="2" Height="10" Width="10" />
When experimenting with designing your path drawing, it's helpful to set the path element's height & width first. To better understand XAML path markup syntax, see MSDN.
You might find it easier if you draw them out on a piece of graph paper and then label the vertices with the values needed to reach it from the previous one.
Then copy this into your code.

WPF - Create buttons with up and down arrows using standard buttons

I want to create some up and down buttons using the standard button
background but with black arrows.
What is the best way to accomplish this with WPF??
Malcolm
I find Marlett (a font built into Windows) handy for that sort of thing.
<Button FontFamily="Marlett" FontSize="20" Content="5"/>
<Button FontFamily="Marlett" FontSize="20" Content="6"/>
Output:
No discussion on this subject would be complete without mentioning the geometry mini-language (or Path Markup Syntax) for a more compact shape definition:-
<Button>
<Path Fill="Black" Data="M 0 6 L 12 6 L 6 0 Z"/>
</Button>
<Button>
<Path Fill="Black" Data="M 0 0 L 6 6 L 12 0 Z"/>
</Button>
The first describes a Move to 0,6 Line to 12,6 Line to 6,0 and then close the shape (Z).
There is also a curve syntax.
The preferred way to do this now is using Segoe UI Symbol. It replaces Marlett and provides many useful glyphs.
Up and down would be
<Button FontFamily="Segoe UI Symbol" Content=""/>
<Button FontFamily="Segoe UI Symbol" Content=""/>
Which renders as:
This font is pre-installed on all versions of Windows 7 and 8.
As of the release of Windows 10, the Segoe UI Symbol font is considered a legacy resource and should be replaced by Segoe MDL2 Assets in all new projects as outlined here.
You can create a Polygon which represents your up and down triangles and then set them to be the content of the buttons:
<Button>
<Polygon
Points="300,200 450,200 375,300 300,200"
Stroke="Black">
<Polygon.Fill>
<SolidColorBrush Color="Black" />
</Polygon.Fill>
</Polygon>
</Button>
You can tweak these to draw different figured, but that's generally the XAML you would use for basic geometry.
If you would like to have an arrow with base rectangle you can use this sample...
<Button >
<Polygon Stretch="Fill" Fill="Black" Points="0,0 0,30 0,10 30,10 30,-10 45,10 30,30 30,20 0,20 0,0 30,0 30,10 0,10" />
</Button>

Resources