c# - Wpf DataGrid 只显示空行

标签 c# wpf data-binding

所以我正在开发从数据库中检索数据的应用程序,我想使用我创建的自定义列在 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/

相关文章:

c# - 不使用保存文件对话框保存文件

c# - ASP.NET - 复合控件 - 在 CreateChildControls 抑制 Button Click 事件后添加动态控件

wpf - 如何在 BackgroundWorker 中打开任何 View ?

wpf - 在 WPF 应用程序中禁用 Aero Peek

c# - 为什么我在这个数据绑定(bind)上得到 "type reference cannot find a public type"?

c# - 从文本框帮助更新 Access 数据库时出现问题?

c# - WP8 点击图片获取longlistselector中的选中项

c#:发送文件,混合成功文件和损坏文件

wpf - 如何使用 token 在 MVVM light 中发送简单的字符串消息

c# - 如何通过 ObjectDataProvider 将 ComboBox 绑定(bind)到通用字典