2015-08-23 30 views
0

數據訪問:列表數據沒有顯示在Datagrid + MVVM上我做錯了什麼?

這是我創建了一個列表項

namespace MVVMTest2.DataAccess 
{ 
    class DataAccessClass 
    { 
     readonly List<EmployeeList> _employeeList; 

     public DataAccessClass() 
     { 
      if (_employeeList == null) 
      { 
       _employeeList = new List<EmployeeList>(); 
      } 
      _employeeList.Add(EmployeeList.CreateEmployee("MD", "Mishu", "M", "72000")); 
      //_employeeList.Add(EmployeeList.CreateEmployee("MD", "Mou", "F", "82000")); 
      //_employeeList.Add(EmployeeList.CreateEmployee("Jill", "Jack", "M", "92000")); 
      //_employeeList.Add(EmployeeList.CreateEmployee("John", "Smith", "M", "85000")); 
      //_employeeList.Add(EmployeeList.CreateEmployee("Amanda", "Scholl", "F", "49000")); 

     } 

     //public List<EmployeeList> GetEmployeeInfo() 

     public ObservableCollection<EmployeeList> GetEmployeeInfo() 
     { 
      // return new List<EmployeeList>(_employeeList); 

      return new ObservableCollection<EmployeeList>(_employeeList); 
     } 

    } 

    internal class EmployeeList 
    { 
     string FirstName; 
     string LastName; 
     string Gender; 
     string Salary; 

     public static EmployeeList CreateEmployee(string firstName, string lastname, string gender, string salary) 
     { 
      return new EmployeeList { FirstName = firstName, LastName = lastname, Gender = gender, Salary = salary }; 
     } 
    } 
} 

模型類一個數據訪問類:

namespace MVVMTest2.Model 
{ 
    class ModelClass 
    { 
     private string _model; 

     private DataAccessClass _data; 

     public ModelClass() 
     { 
     } 

     //public List<EmployeeList> GetDataSet() 
     public ObservableCollection<EmployeeList> GetDataSet() 
     { 
      /* 
      List<string> cities = new List<string>(); 
      cities.Add("New York"); 
      cities.Add("Mumbai"); 
      cities.Add("Berlin"); 
      cities.Add("Istanbul"); 

      // Join strings into one CSV line. 
      string line = string.Join("\n", cities.ToArray()); 
      */ 

      ObservableCollection<EmployeeList> list = new ObservableCollection<EmployeeList>(); 
      DataAccessClass daC = new DataAccessClass(); 
      list = daC.GetEmployeeInfo(); 
      //string line = string.Join("\n", list.ToArray()); 

      return list; 
     } 


     public string GetDataList() 
     { 
      List<string> cities = new List<string>(); 
      cities.Add("New York"); 
      cities.Add("Mumbai"); 
      cities.Add("Berlin"); 
      cities.Add("Istanbul"); 

      // Join strings into one CSV line. 
      string line = string.Join("\n", cities.ToArray()); 
      return line; 
     } 
    } 
} 

視圖模型:

namespace MVVMTest2.ViewModel 
{ 
    class ViewModelAll : ViewModelBase,INotifyPropertyChanged 
    { 
     private ModelClass _model; 

     public ViewModelAll() 
     { 
      _canExecute = true; 
      _model = new ModelClass(); 
     } 

     private string _firstName; 

     public string FirstName 
     { 
      get { return _firstName; } 
      set { _firstName = value; 
       OnPropertyChanged("FirstName"); 
      } 
     } 

     private string _lastName; 

     public string LastName 
     { 
      get { return _lastName; } 
      set 
      { 
       _lastName = value; 
       OnPropertyChanged("LastName"); 
      } 
     } 

     //private List<EmployeeList> _displayName; 
     private ObservableCollection<EmployeeList> _displayName = new ObservableCollection<EmployeeList>(); 
     public ObservableCollection<EmployeeList> DisplayText 
     { 
      get { return _displayName; } 
      set 
      { 
       _displayName = value; 
       OnPropertyChanged("DisplayText"); 
      } 
     } 

     //private List<EmployeeList> _dataGrid; 
     //public List<EmployeeList> DataSet 
     private ObservableCollection<EmployeeList> _dataGrid = new ObservableCollection<EmployeeList>(); 
     public ObservableCollection<EmployeeList> DataSet 
     { 
      get { return _dataGrid; } 
      set 
      { 
       _dataGrid = value; 
       OnPropertyChanged("DataSet"); 
      } 
     } 

     private List<EmployeeList> _dataList; 
     public List<EmployeeList> DataList 
     { 
      get { return _dataList; } 
      set 
      { 
       _dataList = value; 
       OnPropertyChanged("DataList"); 
      } 
     } 

     private bool _canExecute; 

     private ICommand _submitCommand; 

     public ICommand SubmitCommand 
     { 
      get { return _submitCommand ?? (_submitCommand = new CommandHandler(() => MyAction(), _canExecute));} 
     } 

     private ICommand _closeCommand; 

     public ICommand CloseCommand 
     { 
      get { return _closeCommand ?? (_closeCommand = new CommandHandler(() => MyClose(), _canExecute)); } 
     } 

     private void MyClose() 
     { 
      Application.Current.MainWindow.Close(); 
     } 

     public void MyAction() 
     { 
      //MessageBox.Show("you are here"); 
      //FirstName = string.Empty; 
      //OnPropertyChanged("FirstName"); 
      List<string> kala = new List<string>(); 
      //var getdata = _model.GetDataList(); 
      var getdata = _model.GetDataSet(); 
      //var getcity = _model.GetDataList(); 
      DisplayText = getdata; 
      // DataList =getdata; 
      DataSet = getdata; 
      //OnPropertyChanged("DataSet"); 
     } 
    } 
} 

查看:

<DataGrid ItemsSource ="{Binding DataSet}" Name="dataGrid" HorizontalAlignment="Left" Margin="10,158,0,0" 
       VerticalAlignment="Top" Height="70" Width="480" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" Width="120"/> 
     <DataGridTextColumn Header="LastName" Binding="{Binding LastName}" Width="120" /> 
     <DataGridTextColumn Header="Gender" Binding="{Binding Gender}" Width="120"/> 
     <DataGridTextColumn Header="Salary" Binding="{Binding Salary}" Width="*"/> 
    </DataGrid.Columns> 
</DataGrid> 

回答

0

首先,您的ViewModel,ModelDataAccess中不需要有ObservableCollectionObservableCollections用於綁定(主要)用於監視添加/刪除元素的集合。它們通常僅在ViewModel和/或Model內使用。

即便如此,您只需簡單地在您的ViewModelAll類中擁有ObservableCollection即可。

其次,對於不會更改其數據源的屬性,不需要實施INotifyPropertyChanged。您也可以從ViewModelAll課程中刪除該課程。

最後,您的XAML綁定到EmployeeList屬性,但您只有它們定義了ad成員變量。 爲了進行綁定工作,它們必須是屬性(帶有getter和setter)

爲了讓DataGrid綁定起作用(並簡化了它),我重構了一下代碼。看看,如果您有任何問題,請告訴我。

ViewModelAll.cs

namespace WpfApplication1 
{ 
    class ViewModelAll : INotifyPropertyChanged 
    { 
     private ModelClass _model; 

     public ObservableCollection<EmployeeList> DataSet 
     { 
      get; 
      private set; 
     } 

     public ViewModelAll() 
     { 
      _model = new ModelClass(); 
      DataSet = new ObservableCollection<EmployeeList>(); 

      MyAction(); 
     } 


     private void MyClose() 
     { 
      Application.Current.MainWindow.Close(); 
     } 

     public void MyAction() 
     { 
      foreach (var datum in _model.GetDataSet()) 
      { 
       DataSet.Add(datum); 
      } 
     } 

     #region INotifyPropertyChanged 
     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string property) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (PropertyChanged == null) return; 

      handler(this, new PropertyChangedEventArgs(property)); 
     } 
     #endregion INotifyPropertyChanged 
    } 
} 

DataAccessClass.cs

namespace WpfApplication1 
{ 
    class DataAccessClass 
    { 
     readonly List<EmployeeList> _employeeList; 

     public DataAccessClass() 
     { 
      if (_employeeList == null) 
      { 
       _employeeList = new List<EmployeeList>(); 
      } 

      _employeeList.Add(EmployeeList.CreateEmployee("MD", "Mishu", "M", "72000")); 
      _employeeList.Add(EmployeeList.CreateEmployee("MD", "Mou", "F", "82000")); 
      _employeeList.Add(EmployeeList.CreateEmployee("Jill", "Jack", "M", "92000")); 
      _employeeList.Add(EmployeeList.CreateEmployee("John", "Smith", "M", "85000")); 
      _employeeList.Add(EmployeeList.CreateEmployee("Amanda", "Scholl", "F", "49000")); 

     } 

     public List<EmployeeList> GetEmployeeInfo() 
     { 
      return new List<EmployeeList>(_employeeList); 
     } 
    } 

    internal class EmployeeList 
    { 
     public string FirstName { get; private set; } 
     public string LastName { get; private set; } 
     public string Gender { get; private set; } 
     public string Salary { get; private set; } 

     public static EmployeeList CreateEmployee(string firstName, string lastname, string gender, string salary) 
     { 
      return new EmployeeList { FirstName = firstName, LastName = lastname, Gender = gender, Salary = salary }; 
     } 
    } 
} 

ModelClass.cs

namespace WpfApplication1 
{ 
    class ModelClass 
    { 
     private DataAccessClass _data; 
     public ModelClass() 
     { 
      _data = new DataAccessClass(); 
     } 
     public List<EmployeeList> GetDataSet() 
     { 
      return _data.GetEmployeeInfo(); 
     } 
    } 
} 

MainWindow.Xaml.cs

請確保您不要忘記設置DataContext。

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new ViewModelAll(); 
    } 
} 
+0

請給我提供ViewModelall.cs。不幸的是,你發佈了與DataAccessClass相同的代碼 –

+0

@MisuZAMAN查看更新。對不起,複製/粘貼錯誤 –