c# - 与自定义对象的 C# 数据网格绑定
问题描述
我正在尝试在 DataGrid 中显示帐户数据,但绑定似乎无法正常工作。
public ObservableCollection<AccountData> accountlist = new ObservableCollection<AccountData>();
public MainWindow()
{
InitializeComponent();
AccountDataGrid.ItemsSource = accountlist;
}
这就是我的列表的样子以及我如何将它绑定到数据网格。
GetAccountsResponse resp = GetAccountsResponse.GetAccounts(m_authImpl);
List<AccountData> accounts = resp.Accounts;
for (int i = 0; i < accounts.Count; ++i)
{
AccountData account = accounts[i];
accountlist.Add(account);
}
我正在使用 API 来获取帐户数据,然后将其放入我的可观察帐户列表中
<DataGrid x:Name="AccountDataGrid" HorizontalAlignment="Left" Height="249" Margin="10,278,0,0" VerticalAlignment="Top" Width="772" AutoGenerateColumns="False" IsReadOnly="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="Type" Binding="{Binding Path=m_type}"/>
<DataGridTextColumn Header="Number" Binding="{Binding Path=m_number}"/>
<DataGridTextColumn Header="Status" Binding="{Binding Path=m_status}"/>
<DataGridTextColumn Header="isPrimary" Binding="{Binding Path=m_isPrimary}"/>
<DataGridTextColumn Header="isBilling" Binding="{Binding Path=m_isBilling}"/>
<DataGridTextColumn Header="clientAccountType" Binding="{Binding Path=m_clientAccountType}"/>
</DataGrid.Columns>
</DataGrid>
这是我的数据网格的代码
public class AccountData
{
public UserAccountType m_type;
public string m_number;
public AccountStatus m_status;
public bool m_isPrimary;
public bool m_isBilling;
public ClientAccountType m_clientAccountType;
public AccountData();
}
这就是我的对象的样子!
一些背景故事:accountlist 在调试器中有所有正确的信息,但是当需要在数据网格中显示结果时,它显示为空白条目。
public enum UserAccountType
{
Undefined = 0,
Cash = 1,
Margin = 2,
TFSA = 3,
RRSP = 4,
SRRSP = 5,
LRRSP = 6,
LIRA = 7,
LIF = 8,
RIF = 9,
SRIF = 10,
LRIF = 11,
RRIF = 12,
PRIF = 13,
RESP = 14,
FRESP = 15,
FX = 16,
FXD = 17,
Count = 18
}
public enum AccountStatus
{
Undefined = -1,
UnAllocated = 0,
Active = 1,
SuspendedClosed = 2,
SuspendedViewOnly = 3,
LiquidateOnly = 4,
Closed = 5,
Count = 6
}
public enum ClientAccountType
{
Undefined = 0,
Individual = 1,
Joint = 2,
InformalTrust = 3,
Corporation = 4,
InvestmentClub = 5,
FormalTrust = 6,
Partnership = 7,
SoleProprietorship = 8,
Family = 9,
JointAndInformalTrust = 10,
Institution = 11,
Count = 12
}
解决方案
WPF 绑定不支持字段,原因很明显(违反封装)
解决此问题的最简单方法是将公共字段转换为属性。
public class AccountData
{
public UserAccountType m_type { get; set; }
public string m_number { get; set; }
public AccountStatus m_status { get; set; }
public bool m_isPrimary { get; set; }
public bool m_isBilling { get; set; }
public ClientAccountType m_clientAccountType { get; set; }
public AccountData() { /* . . . */ }
}
推荐阅读
- php - php preg_replace,用span标签中的数据属性替换所有span标签
- javascript - 无法读取未定义的属性“first_name”
- android - 导入 JSON 时如何将 GUID 分配给 Firebase 实时数据库中的记录
- sql-server - 带前导零的 SQL Server 填充
- ruby-on-rails - ActiveRecord / Ruby on Rails 中 update_all() 的返回值是多少?
- python - 将字符串从 mac 无线发送到树莓派
- reactjs - 在不更改 JSX 中的组件名称的情况下渲染 HOC(组件)
- javascript - 使用 Rest api 数据动态更新 Highstock 多个系列(Highcharts)
- node.js - 如何禁用 nodejs 套接字/流中的缓冲?
- javascript - 使用对象方法作为事件处理程序