0

在我的项目(带有 MVVM 模式的 Silverligth)中,我有 2 个视图(客户和客户详细信息页面)。在客户页面中,我有一个ListView和一个Button用于打开已在ListView.

ListViewXAML 编码:(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。

4

1 回答 1

2

将要绑定在多个视图模型上的数据提取到一个新类中,例如:

public class CustomerCommonDataViewModel : INotifyPropertyChanged
{
    public Project SelectedProject {get; set;} // Implement it notifying the change.
}

将通知属性添加到两个视图模型(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}
于 2013-01-08T12:45:35.347 回答