从代码隐藏到Silverlight MVVM中的另一个视图获取价值

时间:2013-01-08 12:29:46

标签: silverlight entity-framework mvvm

在我的项目中(带有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。

1 个答案:

答案 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}