首页 > 解决方案 > 与自定义对象的 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
    }

标签: c#objectbindingdatagriddisplay

解决方案


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() { /* . . . */ }
}

推荐阅读