天天看點

從PRISM開始學WPF(番外)共享上下文 RegionContext?

原文: 從PRISM開始學WPF(番外)共享上下文 RegionContext?

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.
從PRISM開始學WPF(番外)共享上下文 RegionContext?

(⊙﹏⊙)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>