2016-06-20 53 views
0

我有一個問題,我建立小型登錄系統,基本上它的準備和努力,但還是有一些問題的用戶界面,SI如果我參加刷新UI在WPF

private void LoadButton_Click(object sender, RoutedEventArgs e) 
    { 
     Nullable<bool> creditencialFile = _controls.CredencialsFileDialog.ShowDialog(); 
     if (creditencialFile == true) 
     { 
      ContextStatic.Filename = _controls.CredencialsFileDialog.FileName; 
      FileInfo creditencialsFileInfo = new FileInfo(ContextStatic.Filename); 
      ContextStatic.RootFolder = creditencialsFileInfo.DirectoryName; 
      model.LeapCreditencials = CredentialHelper.LoadCredentials(ContextStatic.Filename); 
     } 
    } 

它加載這樣的按鈕點擊動作從文件的憑據,並將它們保存在對象屬性:

model.LeapCreditencials = CredentialHelper.LoadCredentials(ContextStatic.Filename); 

現在我想刷新或重新加載UI,所以我所有的信息窗口會被設置爲與新的信息。問題是我需要重新加載每個控件,還是有一個聰明的方式來重新加載新的對象值Ui?

+5

難道你不使用數據綁定? –

+1

如果您在不使用綁定的情況下構建WPF應用程序,那麼最好使用WinForms。當你進行綁定時,建議使用MVVM進行綁定。 – Jai

回答

1

是的,你應該實現INotifyPropertyChanged在模型 msdn description to implement INotify Interface

INotifyPropertyChanged interface用於通知客戶,一般客戶綁定,一個屬性值發生了變化。
當模型的值改變時,它會反映在用戶界面中。

的XAML

<TextBox Text="{Binding Mymodel.CustomerName, 
      UpdateSourceTrigger=PropertyChanged}" /> 

型號

public class DemoCustomer : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public string CustomerName 
    { 
     get 
     { 
      return this.customerNameValue; 
     } 

     set 
     { 

       this.customerNameValue = value; 
       NotifyPropertyChanged(); 
     } 
    } 
+1

綁定應該在視圖和視圖模型層之間使用,而不是在視圖和模型之間使用 – nkoniishvt

+0

在這個之間應該有'viewmodel',這是一個簡潔的答案 – Eldho

+0

我認爲這是令人困惑的,因爲你在綁定中談論「模型」在你的描述中。只是想指出,但你的代碼將工作 – nkoniishvt

-1

是啊,有一個聰明的辦法。它被稱爲MVVM(又名模型視圖視圖模型)。這並不難理解。您只需將視圖綁定到ViewModel中的值,並在更改值時自動更新UI。