简体   繁体   中英

MVVM Binding with exposed property

I have a View that is hooked to a ViewModel. I have a singleton class that's exposed by foo.Current. I have a property on that class that will be changing as the result of a button click command calling a foo.Current function. I want to be able to bind a textbox's text to that value and when that value updates the textbox will update. My ViewModel does implement INotifyPropertyChanged and I have other properties on the view model that update controls fine. What is the best way I can make that singleton class update my ViewModel with the new value? I'm thinking I need to figure out a way to make the Singleton class update the ViewModel.

There's no "best" way in current situation, options are as follows:

  1. Define a property in your viewmodel and pass that viewmodel to your singleton. Singleton should change the relevant property along with it's own.
  2. Move properties from singleton to viewmodel or include singleton in the viewmodel itself
  3. Create an event for changing the property on the singleton
  4. Inherit singleton from INotifyPropertyChanged and in your VM attach to PropertyChanged event of singleton. And monitor your property name.
  5. ...

Easiest one would be the first option though there might be some architectural inconsistencies in your app that should be treated.

如果我理解你,我会使用Observer模式。

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