Prism(七) 区域上下文

源码地址 - WineMonk/PrismStudy: Prism框架学习 (github.com)

人物Model

Person.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class Person : INotifyPropertyChanged
{
#region Properties

private string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
OnPropertyChanged();
}
}

private string _lastName;
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
OnPropertyChanged();
}
}

private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
OnPropertyChanged();
}
}

private DateTime? _lastUpdated;
public DateTime? LastUpdated
{
get { return _lastUpdated; }
set
{
_lastUpdated = value;
OnPropertyChanged();
}
}

#endregion //Properties

#region INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}

#endregion //INotifyPropertyChanged

public override string ToString()
{
return String.Format("{0}, {1}", LastName, FirstName);
}
}

人物列表

PersonList.xaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<UserControl
x:Class="ModuleA.Views.PersonList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid
x:Name="LayoutRoot"
Margin="10"
Background="White">
<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.RegionContext="{Binding SelectedItem, ElementName=_listOfPeople}"
prism:RegionManager.RegionName="PersonDetailsRegion" />
</Grid>
</UserControl>

PersonListViewModel.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class PersonListViewModel : BindableBase
{
private ObservableCollection<Person> _people;
public ObservableCollection<Person> People
{
get { return _people; }
set { SetProperty(ref _people, value); }
}

public PersonListViewModel()
{
CreatePeople();
}

private void CreatePeople()
{
var people = new ObservableCollection<Person>();
for (int i = 0; i < 10; i++)
{
people.Add(new Person()
{
FirstName = String.Format("First {0}", i),
LastName = String.Format("Last {0}", i),
Age = i
});
}

People = people;
}
}

详细信息

PersonDetail.xaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<UserControl x:Class="ModuleA.Views.PersonDetail"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<!-- First Name -->
<TextBlock Text="First Name:" Margin="5" />
<TextBlock Grid.Column="1" Margin="5" Text="{Binding SelectedPerson.FirstName}" />

<!-- Last Name -->
<TextBlock Grid.Row="1" Text="Last Name:" Margin="5" />
<TextBlock Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding SelectedPerson.LastName}" />

<!-- Age -->
<TextBlock Grid.Row="2" Text="Age:" Margin="5"/>
<TextBlock Grid.Row="2" Grid.Column="1" Margin="5" Text="{Binding SelectedPerson.Age}"/>
</Grid>
</UserControl>

PersonDetail.xaml.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public partial class PersonDetail : UserControl
{
public PersonDetail()
{
InitializeComponent();
RegionContext.GetObservableContext(this).PropertyChanged += PersonDetail_PropertyChanged;
}

private void PersonDetail_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var context = (ObservableObject<object>)sender;
var selectedPerson = (Person)context.Value;
(DataContext as PersonDetailViewModel).SelectedPerson = selectedPerson;
}
}

PersonDetailViewModel.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class PersonDetailViewModel : BindableBase
{
private Person _selectedPerson;
public Person SelectedPerson
{
get { return _selectedPerson; }
set { SetProperty(ref _selectedPerson, value); }
}

public PersonDetailViewModel()
{

}
}