0

海我正在做一个使用 MVVM 架构的 silverlight 应用程序。情景是这样的。我在同一页面中有一个组合框和数据网格。我已经使用 ObservableCollection 来绑定 Datagrid 中的值,并且在该集合中我有三个字段,即 Fname、Sname 和 Dept。我在 Combobox 中绑定 Dept,但是如果我选择任何一个部门意味着 DataGrid 中的值不会更新。即,我已经在 ViewModel 中创建了代码,并且我使用 LINQ 查询,并且我还在其中获取了值,

var semp = from s in Employees where s.Dept.Equals(Names.Dept) select s;

我需要将此 semp 数据源发送到 View 中的 Datagrid。数据网格语法是:

<my:DataGrid x:Name="McDataGrid" ItemsSource="{Binding Employees,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Margin="130,151,0,0" Height="137" VerticalAlignment="Top" RowBackground="#AA5D9324" AutoGenerateColumns="True" HorizontalAlignment="Left" Width="196">
        </my:DataGrid>

如果你知道,请帮助我...

4

1 回答 1

0

这是视图模型代码:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Silverlight_MVVM.Model;
using Silverlight_MVVM.Utils;
using System.Linq;
using System.Collections.Generic;


namespace Silverlight_MVVM.ViewModel
{
    public class EmployeeListViewModel:INotifyPropertyChanged
    {
        public ObservableCollection<Employee> Employees { get; private set; }

        public EmployeeListViewModel()
        {
            Employees = Silverlight_MVVM.DataHelper.EmployeeDataHelper.EmployeeData ();

        }
        private string _fname;
        public string Fname
        {
            get
            {
                return _fname;
            }
            set
            {
                _fname = value;
                RaisePropertyChanged("Fname");
            }
        }
        private string _sname;
        public string Sname
        {
            get
            {
                return _sname;
            }
            set
            {
                _sname = value;
                RaisePropertyChanged("Sname");
            }
        }
        private string _dept;
        public string Dept
        {
            get
            {
                return _dept;
            }
            set
            {
                _dept = value;
                RaisePropertyChanged("Dept");
            }
        } 
        private Employee _SelectedEmployee;
        public Employee  SelectedEmployee
        {
            get
            {
                return _SelectedEmployee;
            }
            set
            {
                _SelectedEmployee = value;
                RaisePropertyChanged("SelectedEmployee");
            }
        }
        private string _demp;
        public string demp
        {
            get
            {
                return _demp;
            }
            set
            {
                _demp = value;
                RaisePropertyChanged("demp");
            }
        }

        private Employee _Names;
        public Employee Names
        {
            get
            {
                return _Names;
            }
            set
            {
                _Names = value;

                List<Employee> myList = new List<Employee>();
                IEnumerable<Employee> myEnumerable = myList;
              //  List<Employee> listAgain = myEnumerable.ToList();
               // Employees = (ObservableCollection<Employee>)Employees.Where(_ => _.Dept.Equals(Names.Dept));
                RaisePropertyChanged("Names");
            }
        }

        public void HandleShowMessage()
        {
           // MessageBox.Show("Hello " + Names + ",Welcome to EventTrigger for MVVM.");

        }
        public RelayCommand _AddEmployeeCommand;
        /// <summary>
        /// Returns a command that show the customer.
        /// </summary>
        public ICommand AddEmployeeCommand
        {
            get
            {
                if (_AddEmployeeCommand == null)
                {
                    _AddEmployeeCommand = new RelayCommand(
                        param => this.AddEmployee(),
                        param => this.CanAddEmployee
                        );
                }
                return _AddEmployeeCommand;
            }
        }

        public bool CanAddEmployee
        {
            get
            {
                 return true;
            }
        }


        public void AddEmployee()
        {
            Employee newEmployee = new Employee();
            if (Names == null)
            {
                newEmployee.Fname = this.Fname;
                newEmployee.Sname = this.Sname;
                newEmployee.Dept = this.Dept;
                Employees.Add(newEmployee);
                //SelectedEmployee = newEmployee.ToString();
            }
            else //This is the event for getting selected item through combobox and the linq query fetching
            {
             Employees = Silverlight_MVVM.DataHelper.EmployeeDataHelper.EmployeeData();

             var semp = from emp in Employees where emp.Dept.Equals(Names.Dept) select emp;



            }
        }



        #region INotifyPropertyChanged
        // [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion


    }
}
于 2012-10-18T13:16:35.497 回答