Winform 在用户控件之间传递数据 [英] Winform passing data between user controls

查看:50
本文介绍了Winform 在用户控件之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Windows 窗体应用程序,它有几个用户控件 - 当从列表框中选择相关选项时,每个控件都会显示.

I have windows form application which has several user controls - each one is displayed when the relevant option is selected from a listbox.

某些用户控件需要访问存储在不同用户控件中的数据,因此用户控件 A 需要知道用户控件 B 中存储的文本框的值.我已经完成了用户控件 B 中的一些属性的公开. 当应用程序首次加载并且没有更改任何值时,这一切正常.

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.

我遇到的问题是,如果用户控件 B 中文本框的值发生更改,则用户控件 A 不会选择它.

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.

我必须对 NotifyPropertyChanged 做些什么吗?请问有什么建议吗?

Do I have to do something with NotifyPropertyChanged? Any suggestions please?

推荐答案

这里有两个解决方案:

  • 在更新 Form 对象时创建一系列公共属性并处理传递值.
  • 创建一个 event 以在事情发生变化时进行通信,并在目标 Form 中注册一个事件处理程序以接受更改.这在主题上类似于 INotifyPropertyChanged 接口,但仅适用于正式数据绑定场景.
  • 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.

我更喜欢这类事件.

这篇关于Winform 在用户控件之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆