C#数据网格与自定义对象绑定
我试图在DataGrid中显示帐户数据,但绑定似乎无法正常工作C#数据网格与自定义对象绑定,c#,object,binding,datagrid,display,C#,Object,Binding,Datagrid,Display,我试图在DataGrid中显示帐户数据,但绑定似乎无法正常工作 public ObservableCollection<AccountData> accountlist = new ObservableCollection<AccountData>(); public MainWindow() { InitializeComponent(); AccountDataGrid.ItemsSource = ac
public ObservableCollection<AccountData> accountlist = new ObservableCollection<AccountData>();
public MainWindow()
{
InitializeComponent();
AccountDataGrid.ItemsSource = accountlist;
}
这就是我的物体看起来的样子
一些背景故事:
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
}
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() { /* . . . */ }
}
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() { /* . . . */ }
}
让我们用电脑来做吧。该方法的主要目标是:对数据所做的任何更改都会立即显示在DataGrid
中
您需要一个实现INotifyPropertyChanged
接口的助手类
NotifyPropertyChanged.cs
公共类NotifyPropertyChanged:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
=>PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
然后使从NotifyPropertyChanged
派生的新数据类发生更改。为了与AccountData
类兼容,我为带有AccountData
参数的构造函数添加了一个重载
UIAccountData.cs
公共类UIAccountData:NotifyPropertyChanged
{
私有用户帐户类型m_类型;
私有字符串m_编号;
私人帐户状态m_状态;
私人住宅是主要的;
私人楼宇收费;
私有客户端帐户类型m_ClientAccountType;
公共用户帐户类型
{
get=>m_类型;
设置
{
m_类型=值;
OnPropertyChanged();
}
}
公共字符串号
{
get=>m_数;
设置
{
m_数=数值;
OnPropertyChanged();
}
}
公共帐户状态
{
get=>m_状态;
设置
{
m_状态=值;
OnPropertyChanged();
}
}
公立学校为主
{
get=>m_是主要的;
设置
{
m_isPrimary=值;
OnPropertyChanged();
}
}
公共图书馆收费
{
get=>m_isBilling;
设置
{
m_=价值;
OnPropertyChanged();
}
}
公共ClientAccountType ClientAccountType
{
get=>m_clientAccountType;
设置
{
m_clientAccountType=值;
OnPropertyChanged();
}
}
公共UIAccountData(){}
公共UIAccountData(AccountData帐户)
{
m_类型=account.m_类型;
m_编号=account.m_编号;
m_状态=account.m_状态;
m_isPrimary=account.m_isPrimary;
m_isBilling=account.m_isBilling;
m_clientAccountType=account.m_clientAccountType;
}
public AccountData to AccountData=>new AccountData
{
m_类型=m_类型,
m_编号=m_编号,
m_状态=m_状态,
m_isPrimary=m_isPrimary,
m_isBilling=m_isBilling,
m_clientAccountType=m_clientAccountType,
};
}
然后创建包含集合并用于绑定的主类
MainViewModel.cs
public类主视图模型:NotifyPropertyChanged
{
公共可观测收集(会计清单);;
公共可观测集合帐户列表
{
get=>\u accountlist;
设置
{
_accountlist=值;
OnPropertyChanged();
}
}
公共主视图模型()
{
//演示值
Accountlist=新的ObservableCollection
{
新UIAccountData{Type=UserAccountType.Cash,Number=“123”,Status=AccountStatus.Active,IsPrimary=true,IsBilling=true,ClientAccountType=ClientAccountType.Individual},
新UIAccountData{Type=UserAccountType.Margin,Number=“aaa”,Status=AccountStatus.Active,IsPrimary=false,IsBilling=true,ClientAccountType=ClientAccountType.InformalTrust},
新UIAccountData{Type=UserAccountType.LRRSP,Number=“bbb”,Status=AccountStatus.SuspendedClosed,IsPrimary=true,IsBilling=false,ClientAccountType=ClientAccountType.Partnership},
新UIAccountData{Type=UserAccountType.SRIF,Number=“456”,Status=AccountStatus.suspendedVienly,IsPrimary=false,IsBilling=false,ClientAccountType=ClientAccountType.Institution},
};
AccountData account=new AccountData(){m_type=UserAccountType.FX,m_number=“fff”,m_status=AccountStatus.SuspendedVie,m_isPrimary=true,m_isBilling=false,m_clientAccountType=clientAccountType.Institution};
Accountlist.Add(新UIAccountData(account));
}
}
完整标记
main window.xaml
最后
MainWindow.xaml.cs
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
让我们使用。该方法的主要目标是:对数据所做的任何更改都会立即显示在DataGrid
中
您需要一个实现INotifyPropertyChanged
接口的助手类
NotifyPropertyChanged.cs
公共类NotifyPr
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
}
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() { /* . . . */ }
}