簡體   English   中英

當集合中的項目發生更改時,Linq 數據綁定更新

[英]Linq databinding update when item in collection has changed

當綁定到我的視圖的項目發生更改時,我希望更新數據庫。 我注意到 PropertyChanged 事件在 linq class 中觸發,但是我如何告訴我的視圖模型發生了一些變化? 我使用 Linq class 作為我的 model 所以我不必重新創建它,這是不好的做法嗎? I know that I could create a new property in my Linq class true or false and use that property from my viewmodel but that wouldn't be too efficent since I would have to redo that each time I needed to update the class from SQL.

  • View 和 Model 之間的分離是 ViewModel 的關鍵職責之一。 否則 Model 將被視圖特定屬性和數據格式污染。
  • ViewModel 應該將 Model 包裝為屬性並將它們公開給 View,並且當 View 修改包裝的 ViewModel 屬性時,它可以依次調用 model 特定方法。

因此,不要將您的 linq 類直接綁定到您的視圖,而是通過 ViewModel 中的屬性公開它們。 就個人收集項目通知而言,這里是您可以使用的東西

只是為了讓其他人知道我為這種情況做了什么。 我確實嘗試了 anivas 發布的鏈接並且它有效,但並不像我為我的解決方案提出的那樣簡單。 我將所選項目綁定到我的模型視圖上的一個屬性,因為它是唯一可以由用戶更改的。 在我的屬性的設置器上,我為 notifyProperty 更改了一個處理程序。 請參閱下面的代碼 private CustAccountLocation _selectedStore;

    public CustAccountLocation SelectedStore
    {
        get { return _selectedStore; }
        set {

            _selectedStore = value;
            SelectedStore.PropertyChanged += new PropertyChangedEventHandler(SelectedStore_PropertyChanged);
            NotifyPropertyChanged("SelectedStore");
        }
    }

    void SelectedStore_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        StoreNeedsSave = true;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM