數據訪問:列表數據沒有顯示在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>
請給我提供ViewModelall.cs。不幸的是,你發佈了與DataAccessClass相同的代碼 –
@MisuZAMAN查看更新。對不起,複製/粘貼錯誤 –