简体   繁体   English

如何在运行时创建新区域

[英]How to create new regions at run time

I am developing a feature that allows users to select some data sources. 我正在开发一个允许用户选择一些数据源的功能。 Each data source can have different criteria. 每个数据源可以具有不同的标准。 For example, say the data source is describing house hold items, if the user selects bread, I would give them a combo box to select brand, another to select type ie white, whole grain etc, if they select paint, I would have combo boxes to select color and amount. 例如,假设数据源描述了持有物品,如果用户选择面包,我会给他们一个组合框来选择品牌,另一个选择类型即白色,全谷物等,如果他们选择油漆,我会有组合用于选择颜色和数量的方框。 In other words, each data source type has a dedicated view and view model. 换句话说,每种数据源类型都有一个专用的视图和视图模型。 In my case there are 10 different types of data sources and I have created view models for each type of data source and a corresponding view. 在我的例子中,有10种不同类型的数据源,我为每种类型的数据源和相应的视图创建了视图模型。

In my example I have a DataGrid which contains multiple rows. 在我的示例中,我有一个包含多行的DataGrid。 Each row has a cell dedicated to a data source and a combo box to allow user to change data sources. 每行都有一个专用于数据源的单元格和一个允许用户更改数据源的组合框。 When the data source is changed, a new view model corresponding to the selected type of data source is created. 更改数据源时,将创建与所选数据源类型对应的新视图模型。 I am using the Unity container to create an instance of the view then setting the data context. 我正在使用Unity容器创建视图的实例,然后设置数据上下文。 The data source is defined in the data grid. 数据源在数据网格中定义。 Each row of the data grid is a view model IRowViewModel which contains a property called PrimaryRegionName. 数据网格的每一行都是一个视图模型IRowViewModel,它包含一个名为PrimaryRegionName的属性。 I generate a random 16 character string for each row to be used as the region name. 我为每一行生成一个随机的16个字符的字符串,用作区域名称。 I bind RegionManager.RegionName to my property. 我将RegionManager.RegionName绑定到我的属性。 (see below) (见下文)

xaml XAML

<DataGridTemplateColumn Header="Primary" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ContentControl Grid.Row="1" 
                            prism:RegionManager.RegionName="{Binding PrimaryRegionName}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I create my view and register it with the region using the code below 我使用下面的代码创建我的视图并在区域中注册它

> register the view with the container as a type. This is the active instance for a view model for the region
> "region" here is the region name e.g "000011112222ffff"
container.RegisterType(viewType, region);
> Resolve an instance of the view 
view = _container.Resolve(viewType, region);
_regionManager.RegisterViewWithRegion(region, view.GetType());
_regionManager.Regions[region].Add(view);
(view as System.Windows.FrameworkElement).DataContext = viewModel;

What I'm guessing is that when the view is created, since the data context is set later, there will be no region registered with the region manager for each new row. 我猜测的是,在创建视图时,由于稍后设置了数据上下文,因此每个新行都不会向区域管理器注册区域。 What I'm looking to know is, how do I create regions with names that are defined at run time? 我想知道的是,如何创建具有在运行时定义的名称的区域?

After posting this I realised that all I had to do was create a new Region, set the name and add it to my region manager 在发布之后,我意识到我所要做的就是创建一个新的Region,设置名称并将其添加到我的区域经理

_container.RegisterType(viewType, region);
view = _container.Resolve(viewType, region);
(view as System.Windows.FrameworkElement).DataContext = viewModel;
IRegion r = new Region();
r.Name = region;
_regionManager.Regions.Add(r);
_regionManager.RegisterViewWithRegion(region, view.GetType());
_regionManager.Regions[region].Add(view);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM