This is a migrated thread and some comments may be shown as answers.

Needle is not animated when created in codebehind

1 Answer 113 Views
Gauges
This is a migrated thread and some comments may be shown as answers.
KocSistem
Top achievements
Rank 1
KocSistem asked on 12 May 2009, 07:53 AM
Hi,

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.Indicators.Add(ndl);
                            rsStat.Max = Math.Pow(10, Math.Ceiling(Math.Log10(d)));
                            ndl.Value = d;
                        }

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 12 May 2009, 04:40 PM
Hi,

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" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    Title="Window2" Height="300" Width="300">  
    <Grid Name="layoutRoot">  
          
    </Grid> 
</Window> 

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()  
        {  
            InitializeComponent();  
 
            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;  
            gauge.Items.Add(rsStat);  
 
            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);  
 
            rsStat.Indicators.Add(ndl);  
 
            layoutRoot.Children.Add(rg);  
 
            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);  
            timer.Start();  
        }  
 
        private void Timer_Tick(object sender, EventArgs e)  
        {  
            timer.Stop();  
 
            Random rnd = new Random();  
            double d = rsStat.Min + (rsStat.Max - rsStat.Min) * rnd.NextDouble();  
 
            ndl.Value = d;  
 
            timer.Start();  
        }  
    }  

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.
Tags
Gauges
Asked by
KocSistem
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or