0

In a WPF application, I need to refresh the UI for all components at a specific time interval. I would need to know what is the simplest way.

  • My question: How to rise OnPropertyChanged?

  • Do you know a better approch?

Please provide a sample of code if possible thanks

namespace MyClient.Common
{

    public abstract class BindableBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected bool SetProperty<T>(ref T storage, T value, /*[CallerMemberName]*/ String propertyName = null)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }

        protected void OnPropertyChanged(/*[CallerMemberName]*/ string propertyName = null)
        {
            var eventHandler = this.PropertyChanged;
            if (eventHandler != null)
            {
                eventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • You just call OnPropertyChanged. If you pass no parameter it updates all. You could just do it in the event handler for BackgroundWorker ReportPsrogress. – paparazzo Nov 25 '13 at 12:44
  • Use a timer object - http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx. –  Nov 25 '13 at 12:48
  • Timer + Dispatcher (to call on UI thread) ... or a DispatcherTimer http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.dispatchertimer – Olivier Nov 25 '13 at 12:49
  • Just raising the PropertyChanged event without having changed any properties wouldn't make much sense. Having the UI bound to properties that raise PropertyChanged will already "refresh" it. So all you need to do is to set the properties whenever necessary. – Clemens Nov 25 '13 at 12:53
  • Why update all UI properties when the data possibly hasn't changed? Use standard properties that implement the `INotifyPropertyChanged` interface and then they will update the UI whenever their values change. – Sheridan Nov 25 '13 at 12:54

1 Answers1

1

The definately best approach would be not to refresh the controls at all :-) As I see that you have implemented the INotifyPropertyChanged interface you are not too far from implementing a simple MVVM pattern. If you do that the user interface will automatically update on events from the view model, and your controls should allways be up to date.

This is one of many references as to how to get started on MVVM: http://www.markwithall.com/programming/2013/03/01/worlds-simplest-csharp-wpf-mvvm-example.html

I've just been through the same jurney myself, and once I got the hang of it, I realized how clean and nice it all is.

In some cases you have to call the OnPropertyChanged manually, if for instance a property is deduced from other members or external factors. Then you just call OnPropertyChanged("PropertyName").

Snorre
  • 955
  • 1
  • 5
  • 18
  • 1
    ok understand, in my case the view model take data from an xml file, I would like to know how to update the view model at specific time? could you help me out with that? – GibboK Nov 25 '13 at 13:14
  • 1
    What triggers the need of updating? does the content of the file change? Is there a new file? does the user click a button? or something else? – Snorre Nov 25 '13 at 13:17
  • I have an xml with data from a server using http. When the data in the xml file change I need to update the ui. I need to check for xml changes every 5 minutes. – GibboK Nov 25 '13 at 13:19
  • 1
    above is an excellent post on the DispatcherTimer. Use this to invoke the kode that reads the server file. If you implement an MVVM pattern you should do this in the ViewModel. When you have read the file, update your properties, and the user interface should magically update itself. – Snorre Nov 25 '13 at 13:25
  • Glad I could help, and good luck. btw, if your file is on a window server share you can use the FileSystemWatcher class. Then you don't have to use a timer, but you will get an event whenever the file or folder changes. In my experience the FileSystemWatcher works best on your local machine, but it has support of UNC paths. – Snorre Nov 25 '13 at 13:44