2017-04-10 126 views
1

快速提問:C#Winforms數據綁定

我有一個用戶控件,我傳遞一個業務對象給它,以便進行編輯和保存。保存後,用戶控件不會被銷燬,而是在下一個業務對象傳遞給它之前隱藏。

問題是我不能刷新數據的控制,除非我這樣做:

UsernameTextEdit.DataBindings.Clear(); 
UsernameTextEdit.DataBindings.Add("EditValue", entityBo, "Username", true, DataSourceUpdateMode.OnPropertyChanged); 
LastNameTextEdit.DataBindings.Clear(); 
LastNameTextEdit.DataBindings.Add("EditValue", entityBo, "LastName", true, DataSourceUpdateMode.OnPropertyChanged); 
FirstNameTextEdit.DataBindings.Clear(); 
FirstNameTextEdit.DataBindings.Add("EditValue", entityBo, "FirstName", true, DataSourceUpdateMode.OnPropertyChanged); 

不知道如果我這樣做的權利。我相信,它是一個更好的方式。我其實需要的是某種

UsernameTextEdit.DataBindings.Refresh() 

或類似的東西,因爲控件已綁定將businessObject性質,我需要的是讓他們再次讀取該死的值。

有沒有這樣的事情?我錯過了什麼嗎?

謝謝

==============================

我張貼的BusinessObject的模型雖然我無法捉摸如何做到這一點幫助,因爲它工作正常首次

namespace ServiceManager.Bll.Model 
{ 
    [Table("Users")] 
    public class UsersBo : BaseBo 
    { 
     [Display(Name = "Nume utilizator")] 
     [Required(ErrorMessage = "Numele de utilizator este obligatoriu.")] 
     [MaxLength(100, ErrorMessage = "Numele de utilizator nu poate depăși 100 de caractere.")] 
     public string Username { get; set; } 

     [Display(Name = "Parola")] 
     [Required(ErrorMessage = "Parola este obligatorie.")] 
     [MaxLength(100, ErrorMessage = "Parola nu poate depăși 100 de caractere")] 
     public string Password { get; set; } 

     [Display(Name = "Prenume")] 
     [Required(ErrorMessage = "Prenumele este obligatoriu")] 
     [MaxLength(100, ErrorMessage = "Prenumele nu poate depăși 100 de caractere")] 
     public string FirstName { get; set; } 

     [Display(Name = "Nume")] 
     [Required(ErrorMessage = "Numele este obligatoriu.")] 
     [MaxLength(100, ErrorMessage = "Numele nu poate depăși 100 de caractere.")] 
     public string LastName { get; set; } 

     [Display(Name = "E-mail")] 
     [MaxLength(100, ErrorMessage = "Adresa e-mail nu poate depăși 100 de caractere")] 
     public string Email { get; set; } 

     [Display(Name = "Telefon")] 
     [MaxLength(50, ErrorMessage = "Numărul de telefon nu poate depăși 50 de caractere")] 
     public string Phone { get; set; } 

     [Display(Name = "Activ")] 
     public bool IsActiv { get; set; } 

     [Display(Name = "Administrator")] 
     public bool IsAdmin { get; set; } 

     [ScaffoldColumn(false)] 
     public DateTime LastUpdate { get; set; } 
    } 
} 

======================= ==== 後來編輯:

嗯,你知道當你向某人解釋你的問題時會發生什麼,並在你說話時弄清楚它會發生什麼?

在我的情況是「嘿,我在設置BindingSource的數據源?哦,是的,它是在設計器中設置的。嗯...如果我再次設置它,而不是刪除並添加綁定背部?」並猜測是什麼,工作。

謝謝你們。

+1

發佈您的型號代碼。你的模型應該是'INotifyPropertyChanged' – Emad

+0

在你發佈的代碼中沒有證據表明你的模型對象實現了'INotifyPropertyChanged'或者每個屬性都有'XXXChanged'事件的舊約定。當源值發生變化時,爲了更新綁定的目標,需要一個或另一個。如果這還不足以讓您發現問題,則需要解決問題,以便它包含一個可靠地再現問題的良好[mcve]。 –

+1

@PeterDuniho這個問題似乎與單個屬性綁定無關。看起來好像OP在問如何重新綁定到不同的對象實例。 –

回答

2

問題是,你的綁定綁定到一個具體的實例,因此必須在實例更改時重新創建。

爲了允許一次數據綁定設置,您需要一些間接 - 它可能是一個特殊對象,業務對象是一個可選屬性(例如類似public UsersBo Current { get; set; }的東西,所以您通過嵌套綁定綁定到該對象,如「Current .NAME」等,或通過添加BindingSource組件字段到類使用BindingSource組分用於該目的如下

開始:

private BindingSource bindingSource; 

然後,執行一個時間數據結合建立(在構造或負載事件):

bindingSource = new BindingSource(); 
bindingSource.DataSource = typeof(UsersBo); // to allow binding w/o actual object 
// statically bind the controls to the binding source 
UsernameTextEdit.DataBindings.Add("EditValue", bindingSource, "Username", true, DataSourceUpdateMode.OnPropertyChanged); 
LastNameTextEdit.DataBindings.Add("EditValue", bindingSource, "LastName", true, DataSourceUpdateMode.OnPropertyChanged); 
FirstNameTextEdit.DataBindings.Add("EditValue", bindingSource, "FirstName", true, DataSourceUpdateMode.OnPropertyChanged); 

基本上用替換爲bindingSource。現在,只要你需要選擇一個不同的對象(包括最初的),你可以簡單地做

bindingSource.DataSource = entityBo; 

,這就是它 - 數據綁定的基礎設施港島線照顧的休息。

+0

謝謝。非常好,非常明確的答案,非常感謝。 –