简体   繁体   中英

Linq databinding update when item in collection has changed

I am looking to update the database when an item bound to my View has changed. I noticed that the PropertyChanged event fires in the linq class but how do I tell my viewmodel that something has changed? I am using the Linq class as my model so I don't have to create it all over again, is this bad practice? 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.

  • Separation between View and Model is one of the key resposibilities of ViewModel. Else the Model will be polluted with view specific properies and data formattings.
  • ViewModel should wrap Model as properties and expose them to the View and as the wrapped ViewModel properies are modified by the View, it can in turn call the model specific methods.

Hence don't bind your linq classes directly to your view, rather expose them through properties in ViewModel. As far as individual collection item notification is concerned here is something that you can use.

Just to let anyone else know what I did for this scenario. I did try the link that anivas posted and it worked but wasn't as easy as what I came up with for my solution. I bound the selected item to a property on my modelView since it was the only one that could be changed by the user anyhow. On the setter of my property I put in a handler for the notifyProperty changed. See code below 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM