Needle is not animated when created in codebehind

2 posts, 0 answers
  1. KocSistem
    KocSistem avatar
    25 posts
    Member since:
    Sep 2008

    Posted 12 May 2009 Link to this post


    As you can see in the code below, I create a RadGauge dynamically in the codebehind. The problem is the needles are not animated if I code like in this way. Do I miss something in the code?

    Needle ndl = new Needle();
    ndl.Foreground = new SolidColorBrush(Colors.Yellow);
                            ndl.Background = new SolidColorBrush(Colors.Yellow);
                            RadGauge rg = new RadGauge();
                            rg.Height = 100;
                            rg.Width = 100;
                            ndl.IsAnimated = true;
                            ndl.Duration = new Duration(TimeSpan.FromSeconds(2));
                            rsStat.Min = 0;
                            double d = 0;
                            if (Double.TryParse(Value, out d))
                                rsStat.Label = new LabelProperties();
                                rsStat.Label.Location = ScaleObjectLocation.Inside;
                                rsStat.Label.FontSize = 8;
                                rsStat.Max = Math.Pow(10, Math.Ceiling(Math.Log10(d)));
                                ndl.Value = d;
  2. Andrey
    Andrey avatar
    1681 posts

    Posted 12 May 2009 Link to this post


    I am not really sure about the context this code is used in. You can find below a small example which shows an animated needle, created from code behind. Hope this helps.

    <Window x:Class="WpfGaugeExamples.Window2" 
        Title="Window2" Height="300" Width="300">  
        <Grid Name="layoutRoot">  

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Data;  
    using System.Windows.Documents;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Imaging;  
    using System.Windows.Shapes;  
    using System.Windows.Threading;  
    using Telerik.Windows.Controls;  
    using Telerik.Windows.Controls.Gauges;  
    namespace WpfGaugeExamples  
        /// <summary>  
        /// Interaction logic for Window2.xaml  
        /// </summary>  
        public partial class Window2 : Window  
            /// <summary>  
            /// Timer to change indicator's value  
            /// </summary>  
            private DispatcherTimer timer;  
            /// <summary>  
            /// Radial scale.  
            /// </summary>  
            private RadialScale rsStat;  
            /// <summary>  
            /// Needle.  
            /// </summary>  
            private Needle ndl;  
            public Window2()  
                RadGauge rg = new RadGauge();  
                rg.Height = 100;  
                rg.Width = 100;  
                RadialGauge gauge = new RadialGauge();  
                rg.Content = gauge;  
                rsStat = new RadialScale();  
                rsStat.Min = 0;  
                rsStat.Max = 100;  
                rsStat.IsLogarithmic = true;  
                rsStat.Label = new LabelProperties();  
                rsStat.Label.Location = ScaleObjectLocation.Inside;  
                rsStat.Label.FontSize = 8;  
                ndl = new Needle();  
                ndl.Name = "needle";  
                ndl.Foreground = new SolidColorBrush(Colors.Yellow);  
                ndl.Background = new SolidColorBrush(Colors.Yellow);  
                ndl.IsAnimated = true;  
                ndl.Duration = new Duration(TimeSpan.FromSeconds(2));  
                // Register the name with the page to which the needle belongs.  
                this.RegisterName(ndl.Name, ndl);  
                this.Loaded += new RoutedEventHandler(Window_Loaded);  
            private void Window_Loaded(object sender, RoutedEventArgs e)  
                timer = new DispatcherTimer();  
                timer.Interval = TimeSpan.FromSeconds(2.5);  
                timer.Tick += new EventHandler(Timer_Tick);  
            private void Timer_Tick(object sender, EventArgs e)  
                Random rnd = new Random();  
                double d = rsStat.Min + (rsStat.Max - rsStat.Min) * rnd.NextDouble();  
                ndl.Value = d;  

    The main things you have to do to get animated indicator from code are:
    1. You must set Name property of the indicator.
    ndl.Name = "needle";  

    2. You must register indicator’s name in the page XAML.
    // Register the name with the page to which the needle belongs.  
    this.RegisterName(ndl.Name, ndl); 

    By the way, you definitely need not to recreate label properties every time you change needle value. You also have to add needle to indicators collection only once.
    It looks like you are trying to create gauge with logarithmic scale. The RadGauge has build in support for it. I’ve changed code a bit to demonstrate how it can be done.
    rsStat = new RadialScale();  
    rsStat.Min = 0;  
    rsStat.Max = 100;  
    rsStat.IsLogarithmic = true;  

    All the best,
    Andrey Murzov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top