從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.

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>

上面代碼中即是,將_listOfPeople的SelectedItem,作為即將加載到PersonDetailsRegion中的視圖的RegionContext

PersonListViewModel的People的元素類型是Person
PersonDetailViewModel的公開屬性是SelectedPerson,類型也是Person
這在一些傳統應用,含有列表與詳細的頁面中非常有用。(這個很像winform里的databindings)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM