2010-06-24 106 views
0

我一直在嘗試使用複合應用程序庫(Prism),並且我已經設置了一個非常標準的模式,我已經遵循微軟的教程。基本上,視圖被注入該區域。視圖是動態構建的,以編程方式添加控件等等。重新綁定控件使用Silverlight中的PRISM模式

我有一個命令被觸發,並在回發上我想重新綁定當前視圖上的控件,而不是完全重新渲染所有控件。

所以我試圖更新模型更新版本希望這將強制重新控制。這是行不通的。不知道我應該採取什麼辦法,因爲我是棱鏡新手...

任何想法?

訂閱的事件來處理回傳

IEventAggregator aggregator = this.Container.Resolve<IEventAggregator>(); 
aggregator.GetEvent<DataInstanceLoadedEvent>().Subscribe(this.OnDataInstanceUpdated); 

我想通了如何根據微軟的建議模式重新綁定事件

public void OnDataInstanceUpdated(DataInstance updatedInstance) 
{ 
    if(this.View.Model != null){ 
     // We need to rebind here 
     IRegion region = this.LocateRegion(this.View); // gets the region.... 
     this.View.Model.CurrentDataInstance = updatedInstance; // update the model instance 
    } 
    else{ 
     // Render all controls over again since view.model is null ... 
    } 
} 

回答

0

的實現。

基本上,我所要做的就是繼承我的模型上的INotifyPropertyChanged

然後遵循這個模式,一旦我的模型更新它,然後通過觸發事件通知客戶端該屬性實際上已經改變,從而強制重新綁定所有控件。

public class MyModel : INotifyPropertyChanged 
{ 
    private DataInstance currentDataInstance; 
    public event PropertyChangedEventHandler PropertyChanged; 
    public DataInstance CurrentDataInstance 
    { 
     get 
     { 
      return this.currentDataInstance; 
     } 
     set 
     { 
      if (this.currentDataInstance == value) 
       return; 
      this.currentDataInstance = value; 
      this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentDataInstance")); 
     } 
    } 
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(this, e); 
    } 
}