从PRISM开始学WPF(番外)共享上下文 RegionContext-更新至Prism7.1
RegionContext共享上下文
There are a lot of scenarios where you might want to share contextual information between the view that is hosting a region and a view that is inside a region. For example, a master detail–like view shows a business entity and exposes a region to show additional detail information for that business entity. The Prism Library uses a concept named RegionContext to share an object between the host of the region and any views that are loaded inside the region, as shown in the following illustration.
(⊙﹏⊙)Google一下!
有很多场景可能需要在托管区域的视图和区域内的视图之间共享上下文信息。例如,类似主细节的视图显示一个业务实体并公开一个区域以显示该业务实体的附加详细信息。Prism使用一个名为RegionContext的概念在该区域的主机和该区域内加载的任何视图之间共享一个对象,如下图所示。
大意就是,在父视图中,添加一个Region,用来显示扩展信息,并且指定这个Region的DataContext(但是官方说This approach is somewhat similar to the DataContext, but it does not rely on it.),也就是说,仅仅是像而已!也就是说,不不用再为这个即将加载进来的视图,单独设置DataContext,任何一个加载进来的视图都共享这个RegionContext。
<Grid x:Name="LayoutRoot" Background="White" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox x:Name="_listOfPeople" ItemsSource="{Binding People}"/>
<ContentControl Grid.Row="1" Margin="10"
prism:RegionManager.RegionName="PersonDetailsRegion"
prism:RegionManager.RegionContext="{Binding SelectedItem, ElementName=_listOfPeople}"/>
</Grid>
上面代码中即是,将_listOfPeople的SelectedItem,作为即将加载到PersonDetailsRegion中的视图的RegionContext
PersonListViewModel的People的元素类型是Person
PersonDetailViewModel的公开属性是SelectedPerson,类型也是Person
这在一些传统应用,含有列表与详细的页面中非常有用。(这个很像winform里的databindings)