在我的项目中(带有MVVM模式的Silverligth)我有2个视图(Customer和CustomerDetails页面)。
在客户页面中,我有一个ListView
和一个Button
,用于打开ListView
中已加载的所选项目。
ListView
XAML编码:(Customer.xaml)
<ListBox ItemsSource="{Binding Projects,Mode=TwoWay}"
SelectedItem="{Binding Project,Mode=TwoWay}" SelectionMode="Single">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProjectName,Mode=OneWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Name="CustomerName" Text="{Binding Customer.CustomerName}" />
<TextBlock Name="ProjectName" Text="{Binding Project.ProjectName,Mode=OneWay}"/>
<TextBlock Name="Description" Text="{Binding Project.Description}" />
<HyperlinkButton Content="Open" Click="HyperlinkButton_Click"></HyperlinkButton>
实际上,如果我从ListView
中选择任何projectFile Name,那么单击Hyberlink Button
意味着我将在客户视图中将值绑定到TextBlock
。< / p>
但我的要求是:当我导航到CustomerDetails页面时,要绑定的相同(绑定)值。
客户(代码隐藏)页面:
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
iRegionManager.RegisterViewWithRegion("RootRegion", typeof(View.CustomerDetails));
iRegionManager.RequestNavigate("RootRegion", new Uri("CustomerDetails",
UriKind.Relative));
}
CustomerViewModel.cs
private IEnumerable<Project> projects;
private Project project;
private Customer customer;
SampleDomainContext _context;
public IEnumerable<Project> Projects
{
get { return projects; }
set
{
projects = value;
if (projects != null)
{
Project = projects.FirstOrDefault();
OnPropertyChanged("Project");
}
}
}
public Customer Customer
{
get
{
return customer;
}
set
{
customer = value;
}
}
public Project Project
{
get
{
return project;
}
set
{
project = value;
OnPropertyChanged("Project");
if (project != null)
{
Customer = project.CustomerProjects.FirstOrDefault().Customer;
OnPropertyChanged("Customer");
}
}
}
public CustomerViewModel()
{
int s =Common.ActiveData.Instance.userid;
if(s!=0)
{
UserName = Common.ActiveData.Instance.UserName;
OnPropertyChanged("UserName");
GetProjectList(Common.ActiveData.Instance.userid);
}
}
public void GetProjectList(int userid)
{
_context = new PartsDomainContext();
_context.Load(_context.GetProjectListQuery(ActiveData.Instance.userid), Param =>
{
if (!Param.HasError)
{
Projects = Param.Entities;
OnPropertyChanged("Projects");
}
}, null);
}
在我的CustomerDetailsview页面上,我有以下控件:
正文块 文本块 正文块
[保存按钮] [取消按钮]
我需要将选定的ListBox
值绑定到TextBlocks。
答案 0 :(得分:2)
将要在多个viewmodel上绑定的数据提取到新类中,如:
public class CustomerCommonDataViewModel : INotifyPropertyChanged
{
public Project SelectedProject {get; set;} // Implement it notifying the change.
}
向这个单例的viewmodels(Customer和CustomerDetails)添加一个通知属性,并将传递给同一个CustomerCommonDataViewModel实例。
public class CustomerViewModel : INotifyPropertyChanged
{
public CustomerCommonDataViewModel CommonData {get; set;} // Implement it notifying the change.
}
public class CustomerDetailsViewModel : INotifyPropertyChanged
{
public CustomerCommonDataViewModel CommonData {get; set;} // Implement it notifying the change.
}
在xaml中更改绑定到这个新属性:
{Binding CommonData.Projects}
{Binding CommoData.SelectedProject}