2013-11-26 58 views
1

我需要適應的場景,我有像定時器的東西,並希望改變屬性值反映在用戶界面在一刻(基本上我需要每隔x秒更新用戶界面)。如何將方法添加到ViewModel並從那裏觸發PropertyChanged事件?

我需要知道如何將方法添加到ViewModel並從那裏觸發PropertyChanged事件。

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)); 
       } 
      } 

      public void CallOnPropertyChanged() 
      { 
       // what to add here? 
      } 

     } 
    } 

App.xaml.cs

namespace MyClientWPF 
{ 
    /// <summary> 
    /// Interaction logic for App.xaml 
    /// </summary> 
    public partial class App : Application 
    { 


     private void DispatcherTimer_Tick(object sender, EventArgs e) 
     { 
      App._myDataSource.Load(); 
      App._myDataSource.CallOnPropertyChanged(); 
      // I need to rise OnPropertyChanged here 
     } 

     protected override void OnStartup(StartupEventArgs e) 
     { 


      // timer on the same thread 
      System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
      dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick); 
      dispatcherTimer.Interval = new TimeSpan(0, 0, 20); // 10 seconds 
      dispatcherTimer.Start(); 

      base.OnStartup(e); 
     } 



    } 
} 
+0

要通知哪些屬性? – atomaras

+0

任何屬性,我需要刷新所有UI – GibboK

+2

使OnPropertyChanged公開,並用空的propertyName調用它。空由WPF解釋爲「此對象的所有屬性都已更改」。 – atomaras

回答

0

使用這樣的事情在後面的代碼?

((TestViewModel)this.DataContext).OnPropertyChanged("PropName"); 

您可以直接致電OnPropertyChanged();

但是,它似乎確實有一個更好的方式來完成你想要完成的任務。我寧願嘗試@emedbo的建議。

相關問題