C#/Prism

[C# - Prism] 프리즘 예제 16 - RegionContext

반나무 2023. 9. 2. 09:56

안녕하세요, 반나무입니다.

이번 예제는 RegionContext입니다.

Context는 사전적인 정보로는 문맥, 맥락정도로 순화되지만 컴퓨터분야에서 쉽게 설명하면 현재 사용중인 오브젝트나 로직 정도로 생각해주시면 됩니다.

Context의 뜻을 구글에 검색했을때

RegionContext예제는 Region에서 사용중인 DataContext를 어떻게 변경하는지 알아봅니다.

 

프로그램 구조

프로그램 구조는 다음과 같습니다.

  • ModuleA : 실제 RegionContext가 동작 하는 부분
  • RegionContext : Shell, ModuleA를 참조한다.

ModuleA


ModuleAModule.cs

각 View를 region에 추가합니다.

public class ModuleAModule : IModule
{
    public void OnInitialized(IContainerProvider containerProvider)
    {
        var regionManager = containerProvider.Resolve<IRegionManager>();
        regionManager.RegisterViewWithRegion("ContentRegion", typeof(PersonList));
        regionManager.RegisterViewWithRegion("PersonDetailsRegion", typeof(PersonDetail));
    }

    public void RegisterTypes(IContainerRegistry containerRegistry){}
}

 

PersonList.xaml

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

 

PersonListViewModel.cs

private ObservableCollection<Person> _people;
public ObservableCollection<Person> People
{
    get { return _people; }
    set { SetProperty(ref _people, value); }
}

ListBox에서는 People ObservableCollection를 ItemsSource로 받고있습니다.

ContentControl을 PersonDetailsRegion으로 설정 한 후 해당 RegionContext를 ListBox인 _listofPeople의 SelectedItem으로 지정합니다.

 

 

선택된 SelectedItem은 Person자료형이기 때문에 PersionDetailViewModel에서 Context로 받게됩니다.

변경된 Context로 PersonDetail에서 출력합니다.

SelectedItem에 따른 Region Context변경

 

반응형