How can I create a gauge (i.e. speedometer)?
Specifically, I am trying to build the image on this link.
I am successful at creating a ring. However, now I need to add the ticks to the ring.
XAML:
<Window x:Class="MotoLens.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MotoLens"
mc:Ignorable="d"
Background="Black"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ValueToBrushConverter x:Key="ValueToBrushConverter" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Ellipse Grid.Row="0" Width="300" Fill="Transparent" StrokeThickness="10" Stroke="{Binding ElementName=Slider, Path=Background}" StrokeDashArray="1" StrokeEndLineCap="Square" />
</Grid>
</Window>
In the past, when I had to create custom pie charts and other various things requiring elliptical shapes and arcs, I've used the
Microsoft.Expression.Drawing
library. Just add that reference to your project and you'll get access toArc
, which will do wonders here. The exact same thing can be achieved withPath
,ArcSegment
and various other components, but I just find it easy to work withArc
. Speaking of adding references... that is one of the reasons things like this are handled in Blend, if you're not already doing that, as it automatically brings in those references. I made no assumptions here, so that's why I gave the instructions of adding the reference.So, with that said, here's an example a 10-minute project that shows what you can do:
It goes without saying that there is a lot of static code going on in there, as far as placement of things, but that was done for the demonstration. Most of it is not even necessary, based on what the image of that app in the link you've provided shows, but I'm obsessive about detail and wanted the layout to somewhat match your screenshot in the question. I assume you would create a control out of this, so you'd clean it all up and create appropriate bindings.
As far as the circular gradient topic is concerned, I did not bother working on that here, as that's a completely different subject from what you were asking about and more can be found at a different StackOverflow question right over here: Creating Gradient Brush along a Circular Path