C# 绑定列表<;T>;从类到数据网格
我想在DataGrid中显示列表中的项目。所以我做了一些可能有用的事情 首先,我将DataGrid的C# 绑定列表<;T>;从类到数据网格,c#,wpf,datagrid,C#,Wpf,Datagrid,我想在DataGrid中显示列表中的项目。所以我做了一些可能有用的事情 首先,我将DataGrid的ItemsSource设置为位于类内部的列表,并将自定义列的DataGridTextColumns绑定到列表的项 班级: public class Auftrag : INotifyPropertyCanged { private int _id; // ID is an example to show that there is more then `Services` priv
ItemsSource
设置为位于类内部的列表,并将自定义列的DataGridTextColumns
绑定到列表的项
班级:
public class Auftrag : INotifyPropertyCanged
{
private int _id; // ID is an example to show that there is more then `Services`
private List<Services> _services = new List<Services>();
[...] // There are more Fields
[...] // There are more Properties
public int ID { get; set; } // ID is just an Example. My Properties aren't {get; set;} My properties has all the same structure like `Services` Property
public List<Services> Services
{
get => _services;
set
{
if(_services == value) return;
_services = value;
OnPropertyChanged("Services");
}
}
}
我正在读取数据库并将结果添加到属性:
public async Task<bool> LoadSingleAuftrag(int aID)
{
return await Task.Run(() =>
{
// Check SQL Connection, Run Query etc.
[...]
using(var reader = cmd.ExecuteReader())
{
while(reader.Read())
{
SAuftrag.AuftragsId = reader["AuftragsID"] as int? ?? default(int);
SAuftrag.KundenId = reader["KundenID"] as int? ?? default(int);
SAuftrag.Status = reader["Status"] as string;
SAuftrag.CreatedDate = reader["ErstelltAm"] as DateTime? ?? default(DateTime);
SAuftrag.FinishedDate = reader["FertigGestelltAm"] as DateTime? ?? default(DateTime);
SAuftrag.Services.Add(new Services
{
Servicename = reader["Servicename"] as string,
Servicebeschreibung = reader["Servicebeschreibung"] as string,
Einzelpreis = reader["Preis"] as double? ?? default(double),
Anzahl = reader["Anzahl"] as int? ?? default(int),
Preis = Convert.ToDouble(reader["Preis"]) * Convert.ToInt32(reader["Anzahl"])
});
}
// Is working fine, so the Items are sucessfully stored into the `Services` list.
Debug.WriteLine("Anzahl: " + SAuftrag.Services[0].Anzahl);
Debug.WriteLine("Einzelpreis: " + SAuftrag.Services[0].Einzelpreis);
Debug.WriteLine("Gesamtpreis: " + SAuftrag.Services[0].Preis);
}
}
}
查看.代码隐藏
在代码隐藏中,我调用一个方法来加载数据:
private readonly AuftragViewModel _viewModel;
public AuftragsInfo(int aID)
{
InitializeComponent();
_viewModel = (AuftragViewModel) DataContext;
LoadAuftrag(aID);
}
public async void LoadAuftrag(int aID)
{
if (!await _viewModel.LoadSingleAuftrag(aID))
{
Close();
}
}
@ASh在评论中写道尝试
可观察收集
,而不是列表
。
我不知道为什么,但在将列表
更改为可观察收集
并通过调度程序
添加项目后,它开始工作了
因此,如果有人将来也会犯这种错误,不要再使用List
。我想这是使用ObservableCollection
的最好方法
值得注意的是,由于@ASh.的缘故,类型和属性名可能是相等的,比如
public-Auftrag-Auftrag{get;set;}
。S
代表Single
:)另一个注意事项是,当您在代码中混合语言时,它通常看起来很奇怪。考虑只使用英文标识符名称。是的,这是我的怪癖。我把德语和英语混在一起。服务中的成员的结构与其他属性相同(private-type\u-property;public-type-property{get=>\u-property;set…}
),您确定引发异常的不是属性获取程序吗?我们只能猜测你的代码出了什么问题。。。
<Window.DataContext>
<viewModels:AuftragViewModel />
</Window.DataContext>
[...]
<DataGrid ItemsSource="{Binding SAuftrag.Services}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Servicename" Header="Servicename" Binding="{Binding Path=Servicename}" />
<DataGridTextColumn x:Name="Beschreibung" Header="Beschreibung" Binding="{Binding Path=Servicebeschreibung}" />
<DataGridTextColumn x:Name="Anzahl" Header="Anzahl" Binding="{Binding Path=Anzahl}" />
</DataGrid.Columns>
</DataGrid>
[..]
private double _preis;
private double _einzelpreis;
[..]
public double Preis
{
get => _preis;
set
{
if (_preis == value) return; // Possible loss of precision while rounding values warning
_preis = value;
OnPropertyChanged("Preis");
}
}
public double Einzelpreis
{
get => _einzelpreis;
set
{
if (_einzelpreis == value) return; // Possible loss of precision while rounding values warning
_einzelpreis = value;
OnPropertyChanged("Einzelpreis");
}
}
private readonly AuftragViewModel _viewModel;
public AuftragsInfo(int aID)
{
InitializeComponent();
_viewModel = (AuftragViewModel) DataContext;
LoadAuftrag(aID);
}
public async void LoadAuftrag(int aID)
{
if (!await _viewModel.LoadSingleAuftrag(aID))
{
Close();
}
}