gpt4 book ai didi

c# - Wpf DataGrid 只显示空行

转载 作者:行者123 更新时间:2023-12-05 02:19:51 40 4
gpt4 key购买 nike

所以我正在开发从数据库中检索数据的应用程序,我想使用我创建的自定义列在 DataGrid 中显示它。

XAML:

<DataGrid x: Name = "dataGridAddedAgents" Grid.Column = "0" Grid.ColumnSpan = "7"  ItemsSource = "{Binding}"  HorizontalAlignment = "Stretch" Margin = "10,0" Grid.Row = "3" VerticalAlignment = "Top" AutoGenerateColumns = "False"  IsReadOnly = "True" >
<DataGrid.Columns>
<DataGridTextColumn Binding = "{Binding Path=Id}" Width = "0.25*" Header = "Id" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />
<DataGridTextColumn Binding = "{Binding Path=FirstName}" Width = "0.15*" Header = "First name" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />
<DataGridTextColumn Binding = "{Binding Path=LastName}" Width = "0.15*" Header = "Last name" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />
<DataGridTextColumn Binding = "{Binding Path=Email}" Width = "0.20*" Header = "Email" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />
<DataGridTextColumn Binding = "{Binding Path=Username}" Width = "0.15*" Header = "Username" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />
<DataGridTextColumn Width = "0.10*" ClipboardContentBinding = "{x:Null}" Header = "Remove" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />
</DataGrid.Columns>
</DataGrid>

这是我尝试将数据放入 DataGrid 的 C# 代码

response = await client.GetAsync("api/user/agents");
listOfAgents = await response.Content.ReadAsAsync<ICollection<ApplicationUserModel>>();
dataGridAddedAgents.ItemsSource = listOfAgents;

我已使用调试器进行检查,并将数据正确加载到 listOfAgents 中。当我运行程序时,DataGrid 显示正确的行数,但所有单元格都是空的。

编辑:

 public class ApplicationUserModel
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GuestIdentification { get; set; }
public string Email { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}

类 ApplicationUserModel 看起来像这样。我也用它写入数据库并且它有效灵魂:

从 DataGrid 的列中删除 Foreground = "{x:Null}"修复了它。感谢@mm8 的帮助

最佳答案

When i run program DataGrid shows correct number of rows but all cells are empty.

然后您需要确保 ApplicationUserModel 类包含名为“Id”、“FirstName”、“LastName”、“Email”和“Username”的公共(public)属性(而不是字段),并且这些属性实际上返回预期值。

每个属性至少应该有一个公共(public) setter ,以便您能够绑定(bind)到它们:

public class ApplicationUserModel
{
public string Id {get; set; }
public string FirstName {get; set; }
...
}

编辑:您还应该从 DataGridTextColumns 中删除 Foreground="{x:Null}"。

顺便说一下,您以编程方式设置的 ItemsSource 将“覆盖”您在 XAML 标记中绑定(bind)的 ItemsSource。

关于c# - Wpf DataGrid 只显示空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41021973/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com