将DataTable作为ListView数据源
2016-05-07 16:30
309 查看
将DataTable作为ListView数据源
无废话,看代码:public MainWindow()
{
InitializeComponent();
GridView view = new GridView();
lvw1.View = view;
lvw1.ItemsSource = _table.DefaultView;
GridViewColumn col = new GridViewColumn(); col.Header = "姓名"; col.Width = 150; col.CellTemplate = CreateLabel("Name"); view.Columns.Add(col); col = new GridViewColumn(); col.Header = "性别"; col.Width = 150; col.CellTemplate = CreateLabel("Gender"); view.Columns.Add(col); _table.Columns.Add("Name", typeof(string)); _table.Columns.Add("Gender", typeof(string)); } private DataTable _table = new DataTable(); private void AddDataButton_Click(object sender, RoutedEventArgs e) { _table.Rows.Add("张三", "南"); } private DataTemplate CreateLabel(string prop) { DataTemplate ret = new DataTemplate() { DataType = typeof(TextBlock) }; FrameworkElementFactory fef = new FrameworkElementFactory(typeof(TextBlock)); Binding binding = new Binding(); binding.Path = new PropertyPath(prop); fef.SetBinding(TextBlock.TextProperty, binding); fef.SetValue(Control.ForegroundProperty, new SolidColorBrush(Colors.Red)); ret.VisualTree = fef; return ret; }
相关文章推荐
- Form,tagName和nodeName的区别
- FileSystemWatcher使用方法
- Material Design控件使用(二)
- JSP+Servlet实现注册登录功能
- 直观理解C语言中指向一位数组与二维数组的指针
- swift 泛型
- sigprocmask函数总结
- <html>文档类型
- NYOJ - 会场安排问题
- HDU_1227_Fast Food_动态规划
- 利用arduino 控制舵机转动
- 单调旋转数组的TopK问题
- C++实验5-数组分离
- java的路径研究
- 框架选择的原因及其说明
- Elasticsearch+Logstash+Kibana教程
- 个位数统计
- xml解析-jaxp-sax方式
- Struts2中的struts.i18n.encoding的较量
- 欧拉函数