2014-07-22 11 views
0

저는 C# compact 프레임 워크에서 데이터 바인딩을 사용하고 있습니다. 나는 Textbox와 Label이있는 간단한 폼을 개발한다. Textbox (bindModelTextBox)에 바인드 된 데이터를 변경하고 동일한 데이터에 바인드 된 Label (bindModelLabel)에 의해 이러한 변경 사항을 표시하려고합니다. 여기에 코드입니다 :C# DataBinding이 업데이트되지 않습니다. 컨트롤

public partial class CreateShipment : Form { 

    //simple bean. Just one property: id, a string 
    private BasicShipmentBean toBindBasicShipment = null; 

    public CreateShipment() { 
     InitializeComponent(); 
     BindingSource bsProva = new BindingSource(); 
     toBindBasicShipment = new BasicShipmentBean(); 
     toBindBasicShipment.id = "boo"; 
     bsProva.Add(toBindBasicShipment); 
     bindModelLabel.DataBindings.Add("Text", bsProva, "id", true, DataSourceUpdateMode.OnPropertyChanged); 
     bindModelTextBox.DataBindings.Add("Text", bsProva, "id", true, DataSourceUpdateMode.OnPropertyChanged); 
     bindModelTextBox.LostFocus += textLoseFocus; 
    } 
    ... 
    private void textLoseFocus(object sender, System.EventArgs e) 
    { 
     System.Diagnostics.Debug.WriteLine("focus lost. "+toBindBasicShipment.id); 
    } 

때 텍스트 상자 느슨한 초점 I 데이터 레이블은 여전히 ​​빈의 원래 id 값을 보여줍니다 빈으로 업데이트되지만 볼 수 있습니다. 내가 뭘 놓치고 있니?

+0

혹시이 기능을 사용하셨습니까? 나는 동일한 문제를 가지고있다 - 그리고 나는 INotifyPropertyChanged – uTILLIty

+0

아를 구현했다. CF3.9는 바인딩 될 객체에 의해 직접 명명 된 INotifyPropertyChanged를 필요로한다 - 그것은 기본 클래스에서 인터페이스를 검색하지 않을 것이다. 오 ~ .... – uTILLIty

답변

0

BasicShipmentBean 클래스에 INotifyPropertyChanged를 구현해야합니다. 정확히 원래 이걸 찾은 곳을 잊어 버렸지 만 여기에 모든 데이터 소스에 사용하는 INotifyPropertyChanged를 구현하는 ObservableObject 기본 클래스가 있습니다.

public abstract class ObservableObject : INotifyPropertyChanged 
{ 
    #region Debugging Aides 

    /// <summary> 
    /// Warns the developer if this object does not have 
    /// a public property with the specified name. This 
    /// method does not exist in a Release build. 
    /// </summary> 
    [Conditional("DEBUG")] 
    [DebuggerStepThrough] 
    public virtual void VerifyPropertyName(string propertyName) 
    { 
     // Verify that the property name matches a real, 
     // public, instance property on this object. 
     if (TypeDescriptor.GetProperties(this)[propertyName] == null) 
     { 
      string msg = "Invalid property name: " + propertyName; 

      if (this.ThrowOnInvalidPropertyName) 
       throw new Exception(msg); 
      else 
       Debug.Fail(msg); 
     } 
    } 

    /// <summary> 
    /// Returns whether an exception is thrown, or if a Debug.Fail() is used 
    /// when an invalid property name is passed to the VerifyPropertyName method. 
    /// The default value is false, but subclasses used by unit tests might 
    /// override this property's getter to return true. 
    /// </summary> 
    protected virtual bool ThrowOnInvalidPropertyName { get; private set; } 

    #endregion // Debugging Aides 

    #region INotifyPropertyChanged Members 

    /// <summary> 
    /// Raises the PropertyChange event for the property specified 
    /// </summary> 
    /// <param name="propertyName">Property name to update. Is case-sensitive.</param> 
    public virtual void RaisePropertyChanged(string propertyName) 
    { 
     this.VerifyPropertyName(propertyName); 
     OnPropertyChanged(propertyName); 
    } 

    /// <summary> 
    /// Raised when a property on this object has a new value. 
    /// </summary> 
    public event PropertyChangedEventHandler PropertyChanged; 

    /// <summary> 
    /// Raises this object's PropertyChanged event. 
    /// </summary> 
    /// <param name="propertyName">The property that has a new value.</param> 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     this.VerifyPropertyName(propertyName); 

     PropertyChangedEventHandler handler = this.PropertyChanged; 
     if (handler != null) 
     { 
      var e = new PropertyChangedEventArgs(propertyName); 
      handler(this, e); 
     } 
    } 

    #endregion // INotifyPropertyChanged Members 
} 

그런 다음, 당신은 예를 들어, BasicShipmentBean의 ID에 대한 귀하의 세터에서 OnPropertyChanged를 이벤트를 발생해야

private string _id; 
public string id 
{ 
    get { return _id; } 
    set 
    { 
     if (value != _id) 
      { 
       _id = value; 
       OnPropertyChanged("id"); 
      } 
    } 
} 

데이터, 컴팩트 프레임 워크에 좀 더 지루한 WPF에 비해 바인딩하지만, 구현의 대부분은 꽤 유사합니다.