[英]WPF Refresh Model on binding
我想知道當WPF綁定到對象時,是否有一種方法可以在ViewModel對象上調用方法或更新屬性?
我要這樣做的原因是,當我創建viewModel對象時,它們的數據模型僅包含一個ID,該ID用於在必要時從數據庫查詢數據。 因此,當用戶導航到該對象時,我希望視圖通知ViewModel對象正在監視該對象,結果告訴數據模型從數據庫中更新其值並將ViewModel對象置於加載狀態
如果ViewModel對象知道它們在屏幕上顯示時會自行更新,則可以避免手動刷新所有對象。
謝謝!
當WPF綁定到ViewModel中的對象時,它將使用屬性getter來獲取值。
聽起來您正在嘗試使用惰性評估-只是讓getter延遲實例化數據庫中的信息:
private int entityId; // Set in advance
private Entity entityToFetch; // Will be fetched lazily
public Entity EntityToFetch
{
get
{
if (this.entityToFetch == null) // || this.entityToFetch.Id != this.entityId) - add this if you're letting this change at runtime...
{
this.entityToFetch = DataAccessLayer.FetchEntityForId(this.entityId);
}
return this.entityToFetch;
}
}
您可以將Selected屬性添加到ViewModel中,該屬性在對象被選中時會被設置。 當“選擇的”變為true時,您可以啟動數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.