我做错了什么? 我只是想把返回的List加载到网格视图(顺便说一下,如果你不介意的话,你能给我一些在三层架构中正确应用INotifyPropertyChanged的技巧)
标记:
<Grid Margin="10">
<DataGrid Name="PatientDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="Middle Name" Binding="{Binding MiddleName}" />
<DataGridCheckBoxColumn Header="Status" Binding="{Binding StatusId}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
我的MainWindow代码背后:
public partial class MainWindow : Window
{
PatientMgr koko = new PatientMgr();
public MainWindow()
{
InitializeComponent();
koko.Entity.Id = 0;
koko.EntityList = koko.RetrieveMany(koko.Entity);
DataContext = koko.EntityList;
}
}
我的基类:
public class Patient
{
public int Id { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public bool StatusId { get; set; }
}
我的DataAccessLayer:
public class PatientDb
{
public Patient Retrieve(Patient parameters)
{
Patient item = new Patient();
try
{
var readDb = new PatientDb();
item = readDb.RetrieveMany(parameters).Single();
}
catch (Exception)
{
}
return item;
}
public List<Patient> RetrieveMany(Patient parameters)
{
var items = new List<Patient>();
var command = new SqlCommand();
command.CommandText = "RetrievePatients";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Id", parameters.Id).Direction = ParameterDirection.Input;
command.Parameters.AddWithValue("@LastName", parameters.LastName).Direction = ParameterDirection.Input;
command.Parameters.AddWithValue("@FirstName", parameters.FirstName).Direction = ParameterDirection.Input;
command.Parameters.AddWithValue("@MiddleName", parameters.MiddleName).Direction = ParameterDirection.Input;
DataTable dt = SqlHelper.GetData(command);
foreach (DataRow row in dt.Rows)
{
var item = new Patient();
item.Id = TDefaultValue.GetInt(row["Id"].ToString());
item.LastName = TDefaultValue.GetString(row["LastName"].ToString());
item.FirstName = TDefaultValue.GetString(row["FirstName"].ToString());
item.MiddleName = TDefaultValue.GetString(row["MiddleName"].ToString());
items.Add(item);
items.Add(item);
}
return items;
}
}
我的BusinessLogicLayer
public class PatientMgr : INotifyPropertyChanged
{
private readonly PatientDb _db;
private Patient _entity;
private List<Patient> _entityList;
public Patient Entity
{
get { return _entity; }
set
{
if (Equals(value, _entity)) return;
_entity = value;
OnPropertyChanged();
}
}
public List<Patient> EntityList
{
get { return _entityList; }
set
{
if (Equals(value, _entityList)) return;
_entityList = value;
OnPropertyChanged();
}
}
public PatientMgr()
{
_db = new PatientDb();
Entity = new Patient();
EntityList = new List<Patient>();
}
public Patient Retrieve(Patient parameters)
{
return _db.Retrieve(parameters);
}
public List<Patient> RetrieveMany(Patient parameters)
{
return _db.RetrieveMany(parameters);
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
此外,这些代码是否有标记快捷方式? BTW,SqlHelper和TDefaultValue工作正常。 SqlHelper包含ConnectionString及其相关的任何内容,而TDefaultValue是一个静态类,用于将对象解析/转换为不同的数据类型,以防万一你要问
答案 0 :(得分:0)
我会将您的代码更改为
<Grid Margin="10">
<DataGrid ItemsSource="{Binding EntityList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="Middle Name" Binding="{Binding MiddleName}" />
<DataGridCheckBoxColumn Header="Status" Binding="{Binding StatusId}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
view.cs
public partial class MainWindow : Window
{
PatientMgr koko = new PatientMgr();
public MainWindow()
{
InitializeComponent();
koko.Entity.Id = 0;
koko.EntityList = koko.RetrieveMany(koko.Entity);
DataContext = koko;
}
}