WPF 中可重用列表视图的用户控件
我是一名 winforms 和 asp.net 程序员。 WPF 应用程序开发的新手。
我想提前感谢您对我的查询提供的任何帮助,
我正在处理银行申请。我的工作内容是为应用程序开发可重用的用户控件。
一个例子是创建一个具有最大和最小数字属性的简单数字输入框。因此,如果输入的数字不在范围内,控件会弹出错误。 (当然,这个控件还有更多功能:-)
类似。我必须开发一个具有两个属性的列表视图用户控件。
1) 列标题的逗号分隔值
2) 要添加的逗号分隔数据值
我的同事现在将此用户控件放置在窗口上(如 WPF 中所知)。并将上述参数传递给listview控件。
我的编码确保放置列并添加数据。在 winforms 中这是一个简单的过程。在 WPF 中,我能够通过在 observablecollection 中预先定义或使用其他方式来实现已知列,
SamplelistView.Items.Add(new { FirstName = "test1", LastName = "ABX", EmployeeNumber = "AAA111"});
但是在上述命令中,名字、姓氏是以前已知的列。
是否有任何命令,例如将数据添加为数组或直接将数据添加到列而不绑定到集合。或者有没有办法在运行时将列添加到可观察集合中。
请。请原谅我对术语和其他方面的有限了解。
I am a programmer for winforms and asp.net. A new-comer to WPF application development.
I wish to thank in advance for any help rendered to my query,
I am working on a application for Banking. My job profile is to develop reusable usercontrols for the application.
An example would be to make a Simple numeric input box which has Max and Min number properties. So the control pops an error if the number entered is not within a range. (ofcourse there is much more to this control :-)
On similar lines. I have to develop a listview user control with Two properties.
1) A comma separated values of column headers
2) A comma separated values of data to add
My colleague, now places this usercontrol on window (as known in WPF). and passes the above parameters to the listview control.
My coding ensures that the columns are placed and data is added. In winforms this is a simple process. In WPF i was able to achieve it for known columns by defining beforehand in observablecollection or otherwise using
SamplelistView.Items.Add(new { FirstName = "test1", LastName = "ABX", EmployeeNumber = "AAA111"});
However in the above mentioned command, Firstname,Lastname are previously known columns.
Is there any command like,adding data as a array or adding data directly to columns without binding to a collection. OR is there a way to add columns to observablecollections at runtime.
Pls. excuse my limited exposure on nomenclature and otherwise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决此问题的方法是:
DataGrid
的UserControl
UserControl
添加一个ColumnNames
依赖属性>。在PropertyChanged
事件处理程序中,解析 CSV 并相应地更新DataGrid.Column
。Data
依赖属性添加到UserControl
- 当此更改时,解析 CSV 并将数据添加到DataGrid
。请参阅此问题以获取一些提示:
以编程方式添加列和行rows to WPF Datagrid
顺便说一下,列名的 CSV 是一个可怕的界面!为什么不公开一个
List
,然后添加一个TypeConverter
以便您可以在 XAML 中使用 CSV 字符串?My approach to this problem would be:
UserControl
that hosts aDataGrid
ColumnNames
dependency property to theUserControl
. In thePropertyChanged
event handler, parse the CSV and update theDataGrid.Column
accordingly.Data
dependency property to theUserControl
- and when this changes, parse the CSV and add the data to theDataGrid
.See this question for a few pointers:
programmatically add column & rows to WPF Datagrid
By the way, a CSV of column names is a horrible interface! Why not expose a
List<string>
, then add aTypeConverter
so that you can use a CSV string in XAML?