Winform passing data between user controls

蹲街弑〆低调 提交于 2021-01-29 07:06:23

问题


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 target Form to accept the change. This is similar in theme to the INotifyPropertyChanged 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!