I have a WPF page that contains a Listbox and a frame. The frame has various pages loaded into it determined by the selection within the Listbox.
我有一个包含列表框和框架的WPF页面。该框架具有加载到其中的各种页面,由列表框中的选择确定。
Each page within the frame has a variety of different input boxes and has a Save Cancel button. When the Save button is clicked I need the content to be saved to the database and the Listbox in the parent page to be refreshed to reflect the new data.
框架中的每个页面都有各种不同的输入框,并有一个“保存取消”按钮。单击“保存”按钮时,我需要将内容保存到数据库,并且要刷新父页面中的列表框以反映新数据。
Saving the data is easy but how do I initiate a refresh on the contents of the Listbox in the parent page when calling it from the page that inside the frame?
保存数据很简单,但是当从框架内的页面调用时,如何在父页面中启动列表框内容的刷新?
I need to somehow be able to access the parent pages controls to do this.
我需要以某种方式能够访问父页面控件来执行此操作。
Any ideas?
1 个解决方案
#1
It is technically possible to reach up into the parent control and have your way with the controls it contains, but it makes for code that's very difficult to maintain because if you change the structure of the parent control, you break code in all of the contained pages. That would be considered a very tightly-coupled design and it's often fragile.
技术上可以进入父控件并使用它包含的控件,但它使代码很难维护,因为如果更改父控件的结构,则会破坏所有包含的代码页面。这将被认为是一种非常紧密耦合的设计,而且往往很脆弱。
A somehwat cleaner design would be to have your page classes raise an event when the Save button is pressed. Then your parent frame can sink the event and refresh whatever it knows needs to be refreshed after a save operation. That's easier to maintain because your components are more loosely coupled, but it still puts a lot of database knowledge into your GUI components. Such a design might be appropriate for a relatively simple app on which you don't expect to do a lot of maintenance or future enhancements.
somehwat清洁设计是在按下“保存”按钮时让页面类引发事件。然后,您的父框架可以下沉事件并刷新它在保存操作后需要刷新的任何内容。这更容易维护,因为您的组件更松散耦合,但它仍然将大量数据库知识放入您的GUI组件中。这样的设计可能适用于您不希望进行大量维护或未来增强的相对简单的应用程序。
The design pattern I prefer (as do many developers) is to isolate the database handling and business logic inside one or more classes with a simple programmatic interface that can be tested easily. The GUI components are kept as simple and thin as possible, so they can be easily changed if necessary. This is often called a Model-View-Controller pattern but there are other names for it. In your example, the "controller" class that encapsulates your business logic would have properties and methods for reading and setting information, and a "Save" or "Commit" method that writes changes to a database. Once the save is complete it would raise a "Saved" or "Changed" event that notifies all controls ("views") displaying information that the information has changed and they would refresh themselves based on the new values of the properties of your controller class.
我更喜欢的设计模式(和许多开发人员一样)是使用可以轻松测试的简单编程接口将一个或多个类中的数据库处理和业务逻辑隔离开来。 GUI组件尽可能简单和薄,因此可以根据需要轻松更改。这通常称为模型 - 视图 - 控制器模式,但还有其他名称。在您的示例中,封装业务逻辑的“controller”类将具有用于读取和设置信息的属性和方法,以及将更改写入数据库的“保存”或“提交”方法。保存完成后,它会引发“已保存”或“已更改”事件,通知所有控件(“视图”)显示信息已更改的信息,并根据控制器类属性的新值自行刷新。
#1
It is technically possible to reach up into the parent control and have your way with the controls it contains, but it makes for code that's very difficult to maintain because if you change the structure of the parent control, you break code in all of the contained pages. That would be considered a very tightly-coupled design and it's often fragile.
技术上可以进入父控件并使用它包含的控件,但它使代码很难维护,因为如果更改父控件的结构,则会破坏所有包含的代码页面。这将被认为是一种非常紧密耦合的设计,而且往往很脆弱。
A somehwat cleaner design would be to have your page classes raise an event when the Save button is pressed. Then your parent frame can sink the event and refresh whatever it knows needs to be refreshed after a save operation. That's easier to maintain because your components are more loosely coupled, but it still puts a lot of database knowledge into your GUI components. Such a design might be appropriate for a relatively simple app on which you don't expect to do a lot of maintenance or future enhancements.
somehwat清洁设计是在按下“保存”按钮时让页面类引发事件。然后,您的父框架可以下沉事件并刷新它在保存操作后需要刷新的任何内容。这更容易维护,因为您的组件更松散耦合,但它仍然将大量数据库知识放入您的GUI组件中。这样的设计可能适用于您不希望进行大量维护或未来增强的相对简单的应用程序。
The design pattern I prefer (as do many developers) is to isolate the database handling and business logic inside one or more classes with a simple programmatic interface that can be tested easily. The GUI components are kept as simple and thin as possible, so they can be easily changed if necessary. This is often called a Model-View-Controller pattern but there are other names for it. In your example, the "controller" class that encapsulates your business logic would have properties and methods for reading and setting information, and a "Save" or "Commit" method that writes changes to a database. Once the save is complete it would raise a "Saved" or "Changed" event that notifies all controls ("views") displaying information that the information has changed and they would refresh themselves based on the new values of the properties of your controller class.
我更喜欢的设计模式(和许多开发人员一样)是使用可以轻松测试的简单编程接口将一个或多个类中的数据库处理和业务逻辑隔离开来。 GUI组件尽可能简单和薄,因此可以根据需要轻松更改。这通常称为模型 - 视图 - 控制器模式,但还有其他名称。在您的示例中,封装业务逻辑的“controller”类将具有用于读取和设置信息的属性和方法,以及将更改写入数据库的“保存”或“提交”方法。保存完成后,它会引发“已保存”或“已更改”事件,通知所有控件(“视图”)显示信息已更改的信息,并根据控制器类属性的新值自行刷新。