如何从Silverlight中的ViewModel访问View中的datagrid?

时间:2012-10-18 09:45:40

标签: asp.net silverlight mvvm

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

var semp =来自员工的s,其中s.Dept.Equals(Names.Dept)选择s;

我需要将此semp数据源发送到View中的Datagrid。 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>

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

1 个答案:

答案 0 :(得分:0)

这是ViewModel代码:

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


    }
}