WPF框架访问父页面控件 [英] WPF Frame accessing parent page controls

查看:102
本文介绍了WPF框架访问父页面控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF页面,其中包含一个列表框和一个框架.该框架加载了不同的页面,具体取决于列表框内的选择.

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.

框架中的每个页面都有各种不同的输入框,并具有保存取消"按钮.单击保存"按钮后,我需要将内容保存到数据库中,并且需要刷新父页面中的列表框以反映新数据.

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.

有什么想法吗?

推荐答案

从技术上讲,可以进入父控件并使用包含的控件,但这会使代码难以维护,因为如果您更改父控件的结构,在所有包含的页面中破坏代码.那将被认为是一种紧密耦合的设计,并且通常很脆弱.

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.

一种更加简洁的设计是,当按下保存"按钮时,让您的页面类引发一个事件.然后,父框架可以接收事件并刷新保存操作后需要刷新的所有内容.因为您的组件之间的耦合更松散,所以维护起来更容易,但是它仍然将大量数据库知识带入您的GUI组件中.这种设计可能适用于相对简单的应用程序,您不希望在该应用程序上进行大量维护或将来进行增强.

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.

我喜欢的设计模式(与许多开发人员一样)是使用一个易于编程的简单编程接口将一个或多个类中的数据库处理和业务逻辑隔离开来. 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.

这篇关于WPF框架访问父页面控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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