Linear Gauge: howto position/render the current value on top/bottom of pointer

Posted by: torsten.rendelmann on 14 January 2019, 2:00 am EST

    • Post Options:
    • Link

    Posted 14 January 2019, 2:00 am EST - Updated 4 October 2022, 12:08 am EST

    I try to render dynamically the current value within a TextBlock visually on top of the needle (pointer). See attached screenshot.

    I used a FaceTemplate like this to render the value (currently centered within the LinearGauge (Orientation == Horizontal only):

    
    <DataTemplate x:Key="ValueFaceTemplate">
                        <Border x:Name="displayValue">
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=gauge:C1LinearGauge}, Path=Value,Converter={StaticResource GvtConverter}}"
                                       HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="26" FontWeight="Bold"
                                       />
                        </Border>
    </DataTemplate>
    
    

    I have access to the Border element named “displayValue” from the WPF logical tree and could apply some kind of transformation to correctly position the value at the X-Location of the pointer needle. I have tried various things, but could never get the transition of the text value to the correct location.

    Is this even possible? If so, how to apply that? Can I inherit the C1LinearGauge and get access to some required methods to render this as required? Or can I use a modified face DataTemplate to get it work?

  • Posted 14 January 2019, 7:24 pm EST

    Hello,

    In order to do that you need a container that allows specifying the location as pixel coordinates, like Canvas as follows:```





    Thanks,
    Ruchir
  • Posted 15 January 2019, 3:37 am EST

    OK thank you for the fast repsonse, but what I’m still missing is the code/pseudo code for the Value converter class “MyLocationConverter”: how do I calculate this Left position at the canvas?

    Or is the Value just the position and the converter cast from double to Int??

  • Posted 16 January 2019, 11:46 pm EST

    Hi,

    Whats needed in Converter is the scaling calculation. That is how much is the Gauge pointer moving for every change in Gauge’s Value.

    One easy way to do that is using MultiBinding converter, as follows:```





    <TextBlock.Style>





    <Setter.Value>















    </Setter.Value>





    </TextBlock.Style>



    Thanks,
    Ruchir
    [zip filename="LinearGaugeSample.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-0f19fd54-73aa-4990-b546-859479837ccb.zip[/zip]
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels