问题
I have windows form application which has several user controls - each one is displayed when the relevant option is selected from a listbox.
Some of the user controls need to have access to data stored in a different user control so User Control A needs to know a value of a textbox stored in User Control B. I have done my exposing some properties in the user control B. This all works fine when the application first loads and no values are changed.
The problem I am having is if the value of the textbox in user control B is changed it is not picked up by user control A.
Do I have to do something with NotifyPropertyChanged? Any suggestions please?
回答1:
Two solutions here:
- Create a series of public properties and handle passing values where the
Form
objects are newed up. - Create an
event
to communicate when things change and register an event handler in the targetForm
to accept the change. This is similar in theme to theINotifyPropertyChanged
interface but that's only required/advised for formal databinding scenarios.
I prefer events for this kind of thing.
来源:https://stackoverflow.com/questions/31706759/winform-passing-data-between-user-controls