您應該考慮將Userlist.xaml中的用戶列表綁定到ProfileViewModel實例的集合,然後您可以將特定的ProfileViewModel提供給profile.xaml。
在這個例子中,你Userlist.xaml將包括:
<UserControl Name="userView">
<!-- other stuff -->
<ItemsControl ItemsSource={Binding Users}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding UserName}" />
<Button Content="View User Profile"
Command="{Binding ElementName=userView, Path=DataContext.ViewUserProfileCommand}"
CommandParameter="{Binding}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- other stuff -->
</UserControl>
和你UserlistViewModel將包括:
#region Users Property
public const string UsersPropertyName = "Users";
private ObservableCollection<IProfileViewModelViewModel> _users;
public ObservableCollection<IProfileViewModelViewModel> Users
{
get { return _users; }
set
{
if (_users == value)
return;
_users = value;
RaisePropertyChanged(UsersPropertyName);
}
}
#endregion
public RelayCommand<IProfileViewModel> ViewUserProfileCommand
= new RelayCommand<IProfileViewModel>(ViewUserProfileCommandExecute);
private void ViewUserProfileCommandExecute(IUserProfileViewModel userProfileViewModel)
{
// display your profile view here
}
蘆葦上面提到的,用戶配置文件的視圖模型傳遞到一個方法您的其他頁面將是MVVM Light Toolkit's messaging。