How to: Access Master Detail View and Nested List View Environment 如何:访问主详细信息视图和嵌套列表视图环境

This topic explains how to use Controllers to access a nested List View's or a master Detail View's environment (Frames, Controllers, Actions, Objects, etc.).

本主题介绍如何使用控制器访问嵌套列表视图或主详细信息视图的环境(框架、控制器、操作、对象等)。

This article covers two approaches:

  • How to: Access a Master Detail View's Environment from a Nested List View Controller
  • How to: Access a Nested List View's Environment from a Master Detail View Controller

本文涵盖两种方法:

  • 操作:从嵌套列表视图控制器访问主详细信息视图的环境
  • 操作:从主详细信息视图控制器访问嵌套列表视图的环境

This topic uses the 'Contact Detail View with a Tasks nested List View' scenario. You can find the corresponding Contact and DemoTask business classes in the MainDemo application (%PUBLIC%DocumentsDevExpress Demos 19.2ComponentseXpressApp FrameworkMainDemo).

本主题使用"具有任务嵌套列表视图的接触详细信息视图"方案。您可以在 MainDemo 应用程序中找到相应的联系人和演示任务业务类(%PUBLIC%DocumentsDevExpress Demos 19.2ComponentseXpressApp FrameworkMainDemo).

AccessMasterObject2

Note 注意
XAF creates nested List Views for collection properties. These properties are often used in relationships between persistent objects.
XAF 为集合属性创建嵌套列表视图。这些属性通常用于持久对象之间的关系。

How to: Access a Master Detail View's Environment from a Nested List View Controller

操作:从嵌套列表视图控制器访问主详细信息视图的环境

Choose the best approach for your scenario:

  • Access a Master Detail View's Frame and its Controllers
  • Access a Master Detail View's Current Object (the View Item Approach)
  • Access a Master Detail View's Current Object (the Property Collection Source Approach)

选择适合您的方案的最佳方法:

  • 访问主细节视图的帧及其控制器
  • 访问主详细信息视图的当前对象(视图项方法)
  • 访问主详细信息视图的当前对象(属性集合源方法)

If you need to access the master View from the DashboardViewItem or the DetailPropertyEditor, use the Access a Master Detail View's Frame and its Controllers approach. See the E4916 article for an example.

如果需要从仪表板视图项或详细信息属性编辑器访问主视图,请使用"访问主详细信息视图的帧及其控制器"方法。有关示例,请参阅 E4916 文章。

Access a Master Detail View's Frame and its Controllers

访问主细节视图的帧及其控制器

This approach requires two Controllers: The first is a nested List View Controller. In the code below, the NestedListViewFrameController receives the parent Frame in the AssignMasterFrame method. You can use this Frame to customize its Controllers and Actions. See Customize Controllers and Actions for more information.

此方法需要两个控制器:第一个是嵌套列表视图控制器。在下面的代码中,嵌套列表视图帧控制器在 AssignMasterFrame 方法中接收父帧。您可以使用此框架自定义其控制器和操作。有关详细信息,请参阅自定义控制器和操作。

Note 注意
Nested List View Controllers cannot access the parent Detail View Frame, Controllers, and Actions unless the Detail View's Controller passes the Detail View Frame to the nested List View Controller.
嵌套列表视图控制器无法访问父详细信息视图框架、控制器和操作,除非详细信息视图的控制器将详细视图框架传递给嵌套列表视图控制器。
public class NestedListViewFrameController : ViewController {
    private Frame masterFrame;
    public NestedListViewFrameController() {
        TargetViewType = ViewType.ListView;
        TargetViewNesting = Nesting.Nested;
    }
    public void AssignMasterFrame(Frame parentFrame) {
        masterFrame = parentFrame;
        // Use this Frame to get Controllers and Actions. 
    }
}

The second is a master Detail View Controller. The MasterDetailViewController below acquires the Detail View Frame and passes it to the NestedListViewFrameController. Use the ListPropertyEditor.FrameChanged event subscription to ensure the nested List View exists.

第二个是主细节视图控制器。下面的主详细信息视图控制器获取详细视图帧并将其传递给嵌套列表视图帧控制器。使用 ListPropertyEditor.FrameChanged 事件订阅确保嵌套列表视图存在。

public class MasterDetailViewController : ViewController {
    private void PushFrameToNestedController(Frame frame) {
        foreach (Controller c in frame.Controllers) {
            if (c is NestedListViewFrameController) {
                ((NestedListViewFrameController)c).AssignMasterFrame(Frame);
            }
        }
    }
    private void lpe_FrameChanged(object sender, EventArgs e) {
        PushFrameToNestedController(((ListPropertyEditor)sender).Frame);
    }
    protected override void OnActivated() {
        base.OnActivated();
        foreach (ListPropertyEditor lpe in ((DetailView)View).GetItems<ListPropertyEditor>()) {
            if (lpe.Frame != null) {
                PushFrameToNestedController(lpe.Frame);
            }
            else {
                lpe.FrameChanged += new EventHandler<EventArgs>(lpe_FrameChanged);
            }
        }
    }
    protected override void OnDeactivated() {
        foreach (ListPropertyEditor lpe in ((DetailView)View).GetItems<ListPropertyEditor>()) {
            lpe.FrameChanged -= new EventHandler<EventArgs>(lpe_FrameChanged);
        }
        base.OnDeactivated();
    }
    public MasterDetailViewController() {
        TargetViewType = ViewType.DetailView;
    }
}
Tip 提示
Download the E1012example to see this approach in a sample application.
下载 E1012 示例,在示例应用程序中查看此方法。

Access a Master Detail View's Current Object (the View Item Approach)

访问主详细信息视图的当前对象(视图项方法)

The AccessParentDetailViewController below is a nested List View Controller. The Controller uses the CurrentObjectChanged event to update the master Detail View Caption each time the CurrentObject changes.

下面的访问家长详细信息视图控制器是嵌套列表视图控制器。控制器使用"当前对象更改"事件在每次"当前对象"更改时更新主详细信息视图标题。

public class AccessParentDetailViewController : ViewController {
    private void UpdateDetailViewCaption() {
        if (Frame is NestedFrame) {
            if (View.CurrentObject != null) {
                ((NestedFrame)Frame).ViewItem.View.Caption = ((DemoTask)View.CurrentObject).Subject;
            }
        }
    }
    private void View_CurrentObjectChanged(object sender, EventArgs e) {
        UpdateDetailViewCaption();
    }
    protected override void OnActivated() {
        base.OnActivated();
        View.CurrentObjectChanged += new EventHandler(View_CurrentObjectChanged);
        UpdateDetailViewCaption();
    }
    protected override void OnDeactivated() {
        base.OnDeactivated();
        View.CurrentObjectChanged -= new EventHandler(View_CurrentObjectChanged);
    }
    public AccessParentDetailViewController() {
        TargetViewType = ViewType.ListView;
        TargetViewNesting = Nesting.Nested;
        TargetObjectType = typeof(DemoTask);
    }
}

Tip 提示

Download the E3977example to see this approach in a sample application.

下载 E3977 示例,在示例应用程序中查看此方法。

Access a Master Detail View's Current Object (the Property Collection Source Approach)

访问主详细信息视图的当前对象(属性集合源方法)

The AccessMasterObjectController below is a nested List View Controller. The nested List View's CollectionSource type is PropertyCollectionSource because the List View represents a collection property. The Controller handles the MasterObjectChanged event and processes the current MasterObject when the master object changes.

下面的 AccessMasterObject 控制器是嵌套列表视图控制器。嵌套列表视图的"集合源"类型是属性集合源,因为列表视图表示集合属性。控制器处理主对象更改事件,并在主对象更改时处理当前主对象。

public class AccessMasterObjectController : ViewController<ListView> {
    private void UpdateMasterObject(object masterObject) {
        Contact MasterObject = (Contact)masterObject;
        // Use the master object as required.            
    }
    private void OnMasterObjectChanged(object sender, System.EventArgs e) {
        UpdateMasterObject(((PropertyCollectionSource)sender).MasterObject);
    }
    protected override void OnActivated() {
        base.OnActivated();
        PropertyCollectionSource collectionSource = View.CollectionSource as PropertyCollectionSource;
        if (collectionSource != null) {
            collectionSource.MasterObjectChanged += OnMasterObjectChanged;
            if (collectionSource.MasterObject != null){
                UpdateMasterObject(collectionSource.MasterObject);
            }
        }
    }
    protected override void OnDeactivated() {
        PropertyCollectionSource collectionSource = View.CollectionSource as PropertyCollectionSource;
        if (collectionSource != null) {
            collectionSource.MasterObjectChanged -= OnMasterObjectChanged;
        }
        base.OnDeactivated();
    }
    public AccessMasterObjectController() {
        TargetViewNesting = Nesting.Nested;
        TargetObjectType = typeof(DemoTask);
    }
}
Tip 提示
Download the E950example to see this approach in a sample application.
下载 E950 示例,在示例应用程序中查看此方法。

How to: Access a Nested List View's Environment from a Detail View Controller

操作:从详细视图控制器访问嵌套列表视图的环境

The AccessNestedListViewController below is a Detail View Controller. The Controller handles the the CurrentObjectChanged and ControlCreated events to get the current nested List View's Frame and object. You can process them in the PerformLogicWithCurrentListViewObject and PerformLogicInNestedListViewController methods as the Customize Controllers and Actions topic describes.

下面的 AccessNestedListView 控制器是一个详细信息视图控制器。控制器处理"当前对象更改"和"控制创建"事件以获取当前嵌套列表视图的帧和对象。您可以在"执行逻辑与当前列表视图对象"中处理它们,并按照"自定义控制器和操作"主题所述执行逻辑InInNestslistView控制器方法进行处理。

Note 注意
You can also use this approach to customize the DashboardViewItem and the DetailPropertyEditor because these View Items contain a Frame with an inner View. See the E4916 article for an example.
您还可以使用此方法自定义仪表板视图项和详细信息属性编辑器,因为这些视图项包含具有内部视图的框架。请参阅 E4916文章为例。
 
public class AccessNestedListViewController : ViewController {
    private void PerformLogicWithCurrentListViewObject(Object obj) {
        // Use the object in the nested List View as required.
    }
    private void PerformLogicInNestedListViewController(Frame nestedFrame) {
        // Use the nested Frame as required.
    }
    private void nestedListView_CurrentObjectChanged(object sender, EventArgs e) {
        PerformLogicWithCurrentListViewObject((DemoTask)((ListView)sender).CurrentObject);
    }
    private void listPropertyEditor_ControlCreated(object sender, EventArgs e) {
        ProcessListPropertyEditor((ListPropertyEditor)sender);
    }
    private void ProcessListPropertyEditor(ListPropertyEditor listPropertyEditor) {
        ListView nestedListView = listPropertyEditor.ListView;
        PerformLogicWithCurrentListViewObject(nestedListView.CurrentObject);
        PerformLogicInNestedListViewController(listPropertyEditor.Frame);
        nestedListView.CurrentObjectChanged += new EventHandler(nestedListView_CurrentObjectChanged);
    }
    protected override void OnActivated() {
        base.OnActivated();
        ListPropertyEditor listPropertyEditor = ((DetailView)View).FindItem("Tasks") as ListPropertyEditor;
        if (listPropertyEditor != null) {
            if (listPropertyEditor.Control != null) {
                ProcessListPropertyEditor(listPropertyEditor);
            }
            else {
                listPropertyEditor.ControlCreated += new EventHandler<EventArgs>(listPropertyEditor_ControlCreated);
            }
        }
    }
    protected override void OnDeactivated() {
        ListPropertyEditor listPropertyEditor = ((DetailView)View).FindItem("Tasks") as ListPropertyEditor;
        if (listPropertyEditor != null) {
            listPropertyEditor.ControlCreated -= new EventHandler<EventArgs>(listPropertyEditor_ControlCreated);
        }
        base.OnDeactivated();
    }
    public AccessNestedListViewController() {
        TargetViewType = ViewType.DetailView;
        TargetObjectType = typeof(Contact);
    }
}
Tip 提示
Download the E3977example to see this approach in a sample application.
下载 E3977 示例,在示例应用程序中查看此方法。
原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Access-Master-Detail-View-and-Nested-List-View-Environment.html