Binding Data

4 posts, 0 answers
  1. Sean Pales
    Sean Pales avatar
    3 posts
    Member since:
    Apr 2008

    Posted 16 Mar 2009 Link to this post

    Hi ,
    I am new to Silverlight and trying to use the Frequency Indicator control. I am getting data from the tables using a dataset. Can you advise how i can dynamically bind data to the control. Any sample code that you may have would be great.
    Thanks,
    SP
  2. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 17 Mar 2009 Link to this post

    Hi Sean,

    The “Frequency Indicator” is not a control. It is a sample that demonstrates one possible usage of the RadGauge control, and generally the gauge control does not support databinding beyond binding values to single indicators like needle, bars, etc.

    Regards,
    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.
  3. DevCraft banner
  4. Sean Pales
    Sean Pales avatar
    3 posts
    Member since:
    Apr 2008

    Posted 17 Mar 2009 Link to this post

    Thanks Andrey,
    So i am able to dynamically create number of bars and their height using dynamic values from tables....correct? Do you have some sample code for that?
    SP
  5. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 19 Mar 2009 Link to this post

    Hello Sean Pales,

    Unfortunately Silverlight 2 don’t support DataSet/DataTable, so you can’t use these traditional ADO.NET objects in your Silverlight application. You can use binding to the custom data objects or to the XML instead. For example, you can modify “Frequency Indicator” to bind every linear bar in it to the custom data object.

    First of all you have to create custom data object with double value which will implement INotifyPropertyChanged interface:

    using System;  
    using System.ComponentModel;  
    using System.Net;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Documents;  
    using System.Windows.Ink;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Animation;  
    using System.Windows.Shapes;  
     
    namespace TestGaugeApplication  
    {  
        public class DoubleData : INotifyPropertyChanged  
        {
            #region INotifyPropertyChanged Members  
     
            public event PropertyChangedEventHandler PropertyChanged;
            #endregion  
     
            private double doubleValue;  
     
            public DoubleData()  
            {  
                Value = 0;  
            }  
     
            public double Value  
            {  
                get 
                {  
                    return doubleValue;  
                }  
                set 
                {  
                    doubleValue = value;  
                    NotifyPropertyChanged("Value");  
                }  
            }  
     
            private void NotifyPropertyChanged(string propertyName)  
            {  
                if (PropertyChanged != null)  
                {  
                    PropertyChanged(this,  
                        new PropertyChangedEventArgs(propertyName));  
                }  
            }  
        }  
    }  
     

    Since you need a bunch of bars (32 in case of Frequency Indicator example) be bound to the dynamic data you have to add array of data objects to the code of Frequency Indicator and initialize and array in the page class constructor:

    // Array of data objects  
    private DoubleData[] columnValues = new DoubleData[32];  
     
    public Page()  
    {  
        InitializeComponent();  
     
        // Initialize data objects in array  
        for (int counter = 0; counter < columnValues.Length; counter++)  
        {  
            columnValues[counter] = new DoubleData();  
        }  
     
        CreateBars(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF),  
            Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF));  
     
        this.Loaded += new RoutedEventHandler(Page_Loaded);  
    }  
     

    Then you should modify AddBar method in example so it binds every linear bar to the Value property of the correspondent data object:

    private void AddBar(int i, Color color)  
    {  
        LinearBar bar = new LinearBar();  
        bar.Value = 50d;  
        bar.Background = new SolidColorBrush(color);  
        bar.StrokeThickness = 0d;  
        bar.Location = ScaleObjectLocation.OverOutside;  
        bar.Offset = 0.018 + 0.03998 * 2 * (i - 1);  
        bar.StartWidth = 0.05;  
        bar.EndWidth = 0.05;  
     
        // Bind linear bar to the Value property  
        // of the data object.  
        Binding binding = new Binding();  
        binding.Source = columnValues[i - 1];  
        binding.Path = new PropertyPath("Value");  
        bar.SetBinding(LinearBar.ValueProperty, binding);  
     
        linearScale.Indicators.Add(bar);  
    }  
     

    Now you can change value of the data objects and have linear bars change its value correspondingly. For example:

    Random rnd = new Random();  
     
    for (int counter = 0; counter < barCounter; counter++)  
    {  
        columnValues[counter].Value =   
            linearScale.Min + (linearScale.Max - linearScale.Min) * rnd.NextDouble();  
    }  
     

    Greetings,
    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