在我的项目(带有 MVVM 模式的 Silverligth)中,我有 2 个视图(客户和客户详细信息页面)。在客户页面中,我有一个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>
实际上,如果我从中选择任何 projectFile NameListView
然后单击 HyberlinkButton
意味着我将获得TextBlock
完美绑定到客户视图中的值。
但我的要求是:当我导航到 CustomerDetails 页面时,要绑定相同的(绑定)值。
客户(代码隐藏)页面:
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
iRegionManager.RegisterViewWithRegion("RootRegion", typeof(View.CustomerDetails));
iRegionManager.RequestNavigate("RootRegion", new Uri("CustomerDetails",
UriKind.Relative));
}
客户视图模型.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。