2012-10-18 65 views
0

Hai我正在做一個使用MVVM架構的Silverlight應用程序。 情景是這樣的。我有一個組合框和datagrid在同一頁。 我已經使用ObservableCollection來綁定Datagrid中的值,並且在那個集合中我有三個字段,即Fname,Sname和Dept. 我在Combobox中綁定Dept,但是如果我選擇任何一個部門則意味着該值在DataGrid中不會更新。即,我創建了ViewModel中的代碼,並且我使用了LINQ查詢,並且我也獲取了該值,如何在Silverlight中從ViewModel的View中訪問datagrid?

var semp = from s其中s.Dept.Equals(Names.Dept)select 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> 

幫助我,如果你知道...

+0

您是否實施了您的視圖模型INotifyPropertyChanged的? –

+0

查看或ViewModel? –

+0

是的,我在我的ViewModel中實現了INotifyPropertyChanged .. –

回答

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 


    } 
} 
相關問題