1. 程式人生 > >從PRISM開始學WPF(番外)共享上下文 RegionContext?

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

round hub example out cep oot tail amp margin

原文:從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.

技術分享圖片

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

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