0

我使用简单的 MVVM 框架来创建一个简单的 Silverlight 4.0 LOB 应用程序。

我有一个员工列表视图,其中显示了所有员工的列表,并且我的 EmployeeListViewModel 中有一些属性如下:

    private Grade selectedGrade;
    public Grade SelectedGrade
    {
        get { return selectedGrade; }
        set
        {
            selectedGrade = value;
            NotifyPropertyChanged(m => m.SelectedGrade);
        }
    }

    private Religion selectedReligion;
    public Religion SelectedReligion
    {
        get { return selectedReligion; }
        set
        {
            selectedReligion = value;
            NotifyPropertyChanged(m => m.SelectedReligion);
        }
    }
    private ObservableCollection<Grade> grades;
    public ObservableCollection<Grade> Grades
    {
        get { return grades; }
        set
        {
            grades = value;
            NotifyPropertyChanged(m => m.Grades);
        }
    }

    private ObservableCollection<Religion> religions;
    public ObservableCollection<Religion> Religions
    {
        get { return religions; }
        set
        {
            religions = value;
            NotifyPropertyChanged(m => m.Religions);
        }
    }
    private ObservableCollection<Department> departments;
    public ObservableCollection<Department> Departments
    {
        get { return departments; }
        set
        {
            departments = value;
            NotifyPropertyChanged(m => m.Departments);
        }
    }
    private Employee selectedEmployee;
    public Employee SelectedEmployee
    {
        get { return selectedEmployee; }
        set
        {
            selectedEmployee = value;
            SetCanProperties();
            NotifyPropertyChanged(m => m.SelectedEmployee);
        }
    }
    private ObservableCollection<Employee> employees;   
    public ObservableCollection<Employee> Employees
    {
        get { return employees; }
        set
        {
            employees = value;
            NotifyPropertyChanged(m => m.Employees);
        }
    }
    private Department selectedDepartment;
    public Department SelectedDepartment
    {
        get { return selectedDepartment; }
        set
        {
            selectedDepartment = value;
            NotifyPropertyChanged(m => m.SelectedDepartment);
        }
    }

现在在我看来,我有一个按钮可以在我的员工列表中编辑选定的员工,它会打开一个新的子窗口,其中包含要编辑的 EmployeeDetails

  EmployeeListViewModel viewModel;
    public EmployeeListView()
    {
        InitializeComponent();
        viewModel = (EmployeeListViewModel)DataContext;
    }

这是编辑员工方法

 private void editItemButton_Click(object sender, RoutedEventArgs e)
    {
        // Exit if no product selected
        if (viewModel.SelectedEmployee == null) return;

        // Create a product detail model
       EmployeeDetailViewModel detailModel =
            new  EmployeeDetailViewModel(viewModel.SelectedEmployee);
        // set comboboxes !! 

       detailModel.Departments = viewModel.Departments;
       detailModel.Religions = viewModel.Religions;

       detailModel.Grades = viewModel.Grades;

        // Start editing
        detailModel.BeginEdit();

        // Show EmployeeDetail view
       EmployeeDetailView itemDetail = new  EmployeeDetailView(detailModel);
        itemDetail.Closed += (s, ea) =>
        {
            if (itemDetail.DialogResult == true)
            {
                // Confirm changes

                detailModel.EndEdit();
            }
            else
            {
                // Reject changes
                detailModel.CancelEdit();
            }
        };
        itemDetail.Show();
    }

现在在我的详细信息子视图中我有这个承包商

   public EmployeeDetailView(EmployeeDetailViewModel viewModel)
    {
        InitializeComponent();
        DataContext = viewModel;
    }

这是我的 DetailsViewModel 构造器

   public EmployeeDetailViewModel(Employee model)
    {
        base.Model = model;
    }
         private ObservableCollection<Religion> religions;
    public ObservableCollection<Religion> Religions
    {
        get { return religions; }
        set
        {
            religions = value;
            NotifyPropertyChanged(m => m.Religions);
        }
    }

    private ObservableCollection<Grade> grades;
    public ObservableCollection<Grade> Grades
    {
        get { return grades; }
        set
        {
            grades = value;
            NotifyPropertyChanged(m => m.Grades);
        }
    }
    private ObservableCollection<Department> departments;
    public ObservableCollection<Department> Departments
    {
        get { return departments; }
        set
        {
            departments = value;
            NotifyPropertyChanged(m => m.Departments);
        }
    }

毕竟现在是绑定我有部门,宗教和等级的三个组合框(这是我的员工表中的外键)

 <ComboBox  ItemsSource="{Binding Departments}" DisplayMemberPath="DepartmentName" SelectedValue="{Binding Model.Emp_Department, Mode=TwoWay}" SelectedValuePath="DepartmentId"/>
   <ComboBox  ItemsSource="{Binding Grades}" DisplayMemberPath="GradeName" SelectedValue="{Binding Model.Emp_Grade, Mode=TwoWay}" SelectedValuePath="GradeId"/>

等等..问题是当我更改其值时,只有部门组合框正在更新源值

并且其他组合框不会..即使绑定语句完全相同!

很抱歉写了这么多..但是有人可以帮我吗?

多谢

4

2 回答 2

0

不幸的是,您的 MVVM 分离有点混乱,因为您从 View 直接绑定到底层模型(意味着 ViewModel 中的任何业务逻辑/验证)被绕过。

但是,您似乎已准备就绪,因此我建议您执行以下操作:

将您的 Xaml 更改为此(注意从 SelectedValue 更改为 SelectedItem):

<ComboBox  ItemsSource="{Binding Departments}" DisplayMemberPath="DepartmentName" SelectedItem="{Binding SelectedDepartment, Mode=TwoWay}"/>
<ComboBox  ItemsSource="{Binding Grades}" DisplayMemberPath="GradeName" SelectedItem="{Binding SelectedGrade, Mode=TwoWay}"/>

然后在 SelectedDepartment/SelectedGrade 属性的设置器中,执行任何所需的验证,然后将所选项目的 Id 写入(详细)模型中的属性。

希望能帮助到你。

于 2011-10-12T16:21:14.383 回答
0

好吧,这就是我的组合方式:

 <ComboBox ItemsSource="{Binding Path=ListPeople, UpdateSourceTrigger= PropertyChanged}" SelectedItem="{Binding Path=SelectedPerson, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="FirstName"/>

在我的视图模型中:

 private ObservableCollection<Person> listPeople = new ObservableCollection<Person>();

 public IEnumerable<Person> ListPeople
     {
         get { return this.listPeople; }
     }

 public Person SelectedPerson
     {
         get { return selectedPerson; }
         set
         {
             selectedPerson = value;
             if (selectedPerson != null)
             {
                 NextToPayID = selectedPerson.PersonID;
             }
             base.RaisePropertyChanged("SelectedPerson");
         }
     }

看看你能不能用它来帮忙!

于 2011-11-14T11:18:44.980 回答