Just Code‎ > ‎

Silverlight - Delayed loading of details when selecting an item in some list

posted May 24, 2010, 1:36 AM by Peter Henell
When the selected item is changed in the datagrid, the timer is started.
<data:DataGrid       ItemsSource="{Binding MerchantList, Source={StaticResource vmMerchants}}"
                     SelectedItem="{Binding CurrentMerchant, Mode=TwoWay, Source={StaticResource vmMerchants}}"

private RedeemerInfoEntity _CurrentMerchant;
        public RedeemerInfoEntity CurrentMerchant
        {
            get
            {
                return _CurrentMerchant;
            }
            set
            {
                _CurrentMerchant = value;
                StartDelayedLoadOfDetails();
                OnPropertyChanged("CurrentMerchant");
            }
        }
/// <summary>
        /// Timer used to prevent unwanted webservice calls. Such calls may lock the user inteface if done to frequently.
        /// This problem accurs if the user uses the keyboard to scroll through the MerchantList
        /// </summary>
        private System.Windows.Threading.DispatcherTimer _timer;
        private void StartDelayedLoadOfDetails()
        {
            if (_timer == null)
                _timer = new System.Windows.Threading.DispatcherTimer();
            else
            {
                _timer.Stop();
                _timer.Tick -= new EventHandler(Each_Tick);
            }

            _timer.Interval = new TimeSpan(0, 0, 0, 0, 50);
            _timer.Tick += new EventHandler(Each_Tick);
            _timer.Start();
        }

        /// <summary>
        /// Ticks when the timer have come to its interval
        /// </summary>
        /// <param name="o"></param>
        /// <param name="sender"></param>
        public void Each_Tick(object o, EventArgs sender)
        {
            LoadDetails();
            _timer.Stop();
            _timer.Tick -= new EventHandler(Each_Tick);
        }
Comments