guage wont refresh after update

2 posts, 0 answers
  1. chris
    chris avatar
    4 posts
    Member since:
    Jul 2011

    Posted 06 Sep 2011 Link to this post

    im following the binding example in RadControls Silverlight Courseware. ive attached the project..

    when the timer is called and the stock quote is updated the gauge control does not reflect the change. any suggestion on what i might be doing wrong would greatly be appreciated

    mainpage.xaml

    <UserControl x:Class="Silverlightgauge.MainPage"
             xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Gauge"
             xmlns:abc="clr-namespace:Telerik.Windows;assembly=Telerik.Windows.Controls"   
            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"
            mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
            Loaded="UserControl_Loaded" >

        <UserControl.Resources>
            <LinearGradientBrush x:Key="BackgroundBrush">
                <GradientStop Color="LightBlue" Offset="0.01" />
                <GradientStop Color="SkyBlue" Offset="0.02" />
                <GradientStop Color="SlateBlue" Offset="0.8" />
                <GradientStop Color="SkyBlue" Offset="0.99" />
                <GradientStop Color="LightBlue" Offset="1" />
            </LinearGradientBrush>
        </UserControl.Resources>

        <Grid x:Name="LayoutRoot">
            <telerik:RadGauge >
                <StackPanel Background="{StaticResource BackgroundBrush}"
                        HorizontalAlignment="Stretch">
                    <StackPanel x:Name="spScales"   Orientation="Horizontal"
                            HorizontalAlignment="Center" Margin="10" />
                </StackPanel>
            </telerik:RadGauge>
        </Grid>
    </UserControl>

    code behind

    Imports System.Windows.Controls
    Imports System.Windows.Data
    Imports System.Windows.Media
    Imports System.Windows.Threading
    Imports Telerik.Windows.Controls
    Imports Telerik.Windows.Controls.Data
    Imports Telerik.Windows.Controls.Gauges



    Partial Public Class MainPage
        Inherits UserControl

        Private _random As New Random()
        Private _timer As New DispatcherTimer()
        Private _stocks As New Stocks()

        Public Sub New()
            InitializeComponent()
        End Sub


        Private Sub UserControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' Create a stack panel for each stock and populate
            ' with text label and linear bar
            For Each stock As Stock In _stocks
                ' initialize a linear scale
                Dim scale As New LinearScale()
                scale.Min = 0
                scale.Max = 100
                scale.Width = 70
                ' add a linear bar, already bound to the stock
                scale.Indicators.Add(GetLinearBar(stock))
                ' create a label to display stock symbol
                Dim tb As New TextBlock()
                tb.FontWeight = System.Windows.FontWeights.Bold
                tb.Text = stock.Symbol
                ' initialize a stack panel to contain a text label
                ' and scale. Add the stack panel to the stack panel
                ' already in the xaml markup
                Dim stackPanel As New StackPanel()
                stackPanel.VerticalAlignment = VerticalAlignment.Stretch
                stackPanel.Children.Add(tb)
                stackPanel.Children.Add(scale)
                spScales.Children.Add(stackPanel)
            Next stock
            ' initialize and start a timer to update the stocks
            AddHandler _timer.Tick, AddressOf _timer_Tick
            _timer.Interval = New TimeSpan(0, 0, 0, 1)
            _timer.Start()
        End Sub

        Private Function GetLinearBar(ByVal stock As Stock) As LinearBar
            Dim linearBar As New LinearBar()
            linearBar.Value = stock.Quote
            linearBar.Background = New SolidColorBrush(GetRandomColor())
            linearBar.StartWidth = 0.1
            linearBar.EndWidth = 0.1
            linearBar.RelativeHeight = 0.9
            ' Bind linear bar to the Quote property of the data object.
            Dim binding As New Binding()
            binding.Source = stock
            binding.Path = New PropertyPath("Quote")
            binding.Mode = BindingMode.OneWay
           
            linearBar.SetBinding(linearBar.ValueProperty, binding)

            Return linearBar
        End Function

        Private Function GetRandomColor() As Color
            Return Color.FromArgb(150, CByte(_random.Next(0, 255)), _
            CByte(_random.Next(0, 255)), CByte(_random.Next(0, 255)))
        End Function

        Private Sub _timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
            For Each stock As Stock In _stocks ' update the symbols
            
                stock.Quote = _random.Next(0, 100)
               
             
            Next stock
        End Sub
    End Class

    stock.vb

    Imports System.ComponentModel

    Public Class Stock
        Implements INotifyPropertyChanged

        Public Event PropertyChanged As PropertyChangedEventHandler
        Private _quote As Double
        Private _symbol As String
        Public Sub New(ByVal symbol As String, ByVal quote As Double)
            Me.Quote = quote
            Me.Symbol = symbol
        End Sub
        Public Property Quote() As Double
            Get
                Return _quote
            End Get
            Set(ByVal value As Double)
                _quote = value
                NotifyPropertyChanged("Quote")
            End Set
        End Property
        Public Property Symbol() As String
            Get
                Return _symbol
            End Get
            Set(ByVal value As String)
                _symbol = value
                NotifyPropertyChanged("Symbol")
            End Set
        End Property
        Private Sub NotifyPropertyChanged(ByVal propertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub

        Public Event PropertyChanged1(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    End Class


    stocks.vb

    Public Class Stocks
        Inherits List(Of Stock)
        Public Sub New()
            Dim symbols As List(Of String) = _
            New List(Of String)(New String() {"MSFT", "GOOG", "YHOO", "IBM", "AAPL"})
            Dim random As New Random()
            For Each symbol As String In symbols
                Me.Add(New Stock(symbol, random.Next(0, 100)))
            Next symbol
        End Sub
    End Class





    thanks

    chris
  2. Ves
    Admin
    Ves avatar
    2879 posts

    Posted 09 Sep 2011 Link to this post

    Hi Chris,

    The reason ins in the INotifyPropertyChanged implementation. There is an event PropertyChanged in the Stock class, and there is another one, named PropertyChanged1 which actually implements the INotifyPropertyChanged.PropertyChanged and this seems to lead this undesired behavior. You can strip all the gauge-related obejcts and bind the Text property of a TextBlock - you will still see the same behavior. Changing the Stock class fixes it. Here is the code:

    Public Class Stock
        Implements INotifyPropertyChanged
     
        'Public Event PropertyChanged As PropertyChangedEventHandler
        Private _quote As Double
        Private _symbol As String
        Public Sub New(ByVal symbol As String, ByVal quote As Double)
            Me.Quote = quote
            Me.Symbol = symbol
        End Sub
        Public Property Quote() As Double
            Get
                Return _quote
            End Get
            Set(ByVal value As Double)
                _quote = value
                NotifyPropertyChanged("Quote")
            End Set
        End Property
        Public Property Symbol() As String
            Get
                Return _symbol
            End Get
            Set(ByVal value As String)
                _symbol = value
                NotifyPropertyChanged("Symbol")
            End Set
        End Property
        Private Sub NotifyPropertyChanged(ByVal propertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub
     
        Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    End Class


    Kind regards,
    Ves
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. DevCraft banner
Back to Top