2015-06-29 152 views
2

可能不是問題的正確的標題,但我會盡量解釋: 我的文本框已經綁定到像一個屬性:動態綁定?

class MyViewModel : INotifyPropertyChanged 
    public string TextVal 
    { 
     get 
     { 
      if (View != null) 
        { 
         return View.Model.TextForValueField; // other satrts 
        } 
       return defaultModel.TextForValueField;// first start 

     set 
     { 
      .....//some setters logic with View.Model.TextForValueField 
     } 
    } 

當程序啓動的觀點是不開這樣的吸氣結合的價值字段設置爲默認模式中的值,但這是正確的,但是當我打開新的View 時,我的getter正確返回對應的View.Model.TextForValueField的值,但我的windows功能區菜單上的TextBox字段顯示默認模型(?) 爲什麼?? 綁定的XAML是:

<Setter Property="Text" Value="{Binding MyViewModel.TextVal, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 

也許有一種方法叫吸氣視圖開始時一次?像「刷新」的絲帶?

我的屬性更改的功能

protected void RaisePropertyChanged(string propName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propName)); 
      } 
     } 
+1

如果您的屬性以編程方式更改,您應該實現INotifyPropertyChanged接口以更新數據綁定GUI控件。 – mcy

+0

讓我嘗試重寫您的問題,當'TextForValueField'被更改時,您的'TextBox'(綁定到'TextVal')沒有更新,對不對? – Sinatr

+1

您正在'set'訪問器中調用您的'RaisePropertyChanged'方法,對吧? – mcy

回答

1

當您通過您必須手動對其進行更新視圖模型屬性向前 Model屬性。

public class Model 
{ 
    public string Property {get; set;} 
} 

public class ViewModel 
{ 
    public string BindProperty 
    { 
     get { return modelInstance.Property; } 
     set 
     { 
      modelInstance.Property = value; 
      OnPropertyChanged(); 
     } 
    } 
} 

當您綁定到BindProperty您的視圖時Model.Property得到改變不通知

一個可能的解決方案是讓Model也執行INotifyPropertyChanged,並轉發該通知。另一個(不建議)直接綁定到Model.Property(模型仍然必須實現INotifyPropertyChanged)。

public class Model: NotifyPropertyChangedBase 
{ 
    private string _property; 
    public string Property 
    { 
     get { return _property; } 
     set 
     { 
      _property = value; 
      OnPropertyChanged(); 
     } 
    } 
} 

public class ViewModel: NotifyPropertyChangedBase 
{ 
    public string BindProperty 
    { 
     get { return modelInstance.Property; } 
     set 
     { 
      modelInstance.Property = value; 
      OnPropertyChanged(); 
     } 
    } 

    public ViewModel() 
    { 
     modelInstance.PropertyChanged += Model_PropertyChanged; 
    } 

    void Model_PropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     // forward change notification to View 
     if(e.PropertyName == "Property") 
      OnPropertyChanged("BindProperty"); 
    } 
} 

public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged([CallerMemberName] string property = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
    } 
} 
+0

你能否提供你的OnPropertyChanged函數的代碼?現在看來我的代碼在這個函數中持續循環=( – curiousity

+0

請參閱編輯。也許你以某種方式調用遞歸?你可以問*關於該問題的新問題,只要確保在[MCVE](http: //www.stackoverflow.com/help/mcve)格式 – Sinatr

+0

..沒有這個也沒有保存我的應用程序值仍然凍結 - 它似乎(可能)不正確的轉發更改通知查看我的情況 – curiousity