将二维字符串数组作为WPF ListView的ItemSource
来源:互联网 发布:数控pmc编程 编辑:程序博客网 时间:2024/05/17 20:34
在WPF中,平时我们使用绑定,是将一个对象数组当作数据源,而对象又有很多属性,间接的相当于二维数组,但这样做我们事先要知道对象有什么属性,有几个属性才能绑定,万一不确定ListView的列有多少行,需要在运行时才能确定呢?举例如下:
第一张图:
第二张图:
这两张图的数据的列数不一样,还有可能有其他列数不一样的文件数据,现在的需求是:要求我们的WPF ListView能够载入文件,然后显示出来。
因为有不确定性,所以无法事先定义Model来绑定到ListView上去。如果是Winform的Listview可以一列一列的赋值,但是WPF怎么实现呢?请看:
首先我们要获取列名:用一个string的数组表示好了,用一个二维string数组表示数据:
string[] headers = new string[] { "姓名", "年龄", "身高" };string[][] datas = new string[][] { new string[] { "111","1","165"}, new string[] { "222","2","165"} };
要注意headers 的长度与datas的第二个维度的长度要一样。
本示例前台代码:
<Window x:Class="二维数组绑定ListView.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:二维数组绑定ListView" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> <Grid> <ListView Name="lv"></ListView> </Grid></Window>
动态创建ListView的列(根据headers的长度):
private void GenerateColumns(ListView listView, string[] headers) { if (headers == null || headers.Length == 0) return; GridView gridView = new GridView(); listView.View = gridView; for (int i = 0; i < headers.Length; i++) { GridViewColumn column = new GridViewColumn(); column.Header = headers[i];//设置列名称 //此处设置数组的绑定,数组取第0个元素为list[0],通过索引器取 column.DisplayMemberBinding = new Binding(string.Format("[{0}]", i)); gridView.Columns.Add(column); } }
其中最重要的一句是:column.DisplayMemberBinding = new Binding(string.Format(“[{0}]”, i));动态绑定字符串数组里的元素
全部前台代码贴出来如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace 二维数组绑定ListView{ /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { string[] headers = new string[] { "姓名", "年龄", "身高" }; string[][] datas = new string[][] { new string[] { "111","1","165"}, new string[] { "222","2","165"} }; //根据Header长度生产列 GenerateColumns(lv, headers); //设置数据源 lv.ItemsSource = datas; } private void GenerateColumns(ListView listView, string[] headers) { if (headers == null || headers.Length == 0) return; GridView gridView = new GridView(); listView.View = gridView; for (int i = 0; i < headers.Length; i++) { GridViewColumn column = new GridViewColumn(); column.Header = headers[i];//设置列名称 //此处设置数组的绑定,数组取第0个元素为list[0],通过索引器取 column.DisplayMemberBinding = new Binding(string.Format("[{0}]", i)); gridView.Columns.Add(column); } } }}
运行结果:
增加header的列:
private void Window_Loaded(object sender, RoutedEventArgs e) { //string[] headers = new string[] { "姓名", "年龄", "身高" }; //string[][] datas = new string[][] //{ // new string[] { "111","1","165"}, // new string[] { "222","2","165"} //}; string[] headers = new string[] { "姓名", "年龄", "身高","体重","学历","户籍" }; string[][] datas = new string[][] { new string[] { "111","1","165","45KG","本科","湖北"}, new string[] { "222","2","165","45KG","大专","湖南"} }; //根据Header长度生产列 GenerateColumns(lv, headers); //设置数据源 lv.ItemsSource = datas; }
运行结果如下:
这样就解决了列不确定时,需要用ListView动态显示和绑定数据的问题
阅读全文
0 0
- 将二维字符串数组作为WPF ListView的ItemSource
- WPF ItemSource绑定的问题
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 转:如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- 如何将二维数组作为函数的参数传递
- C++学习笔记--临时对象
- java反射学习笔记(2)----java中的静态加载类和动态加载类
- 5.9
- Win10+Qt5.8+OpenCV3.2配置过程
- <T extends Comparable<? super T>>泛型类型与<T extends Comparable<T>>的区别以及优越性
- 将二维字符串数组作为WPF ListView的ItemSource
- IDEA注释样例笔记
- C3P0连接池
- VisualSFM+MeshLab 三维重建全过程记录
- c语言练习题
- Python函数的用法总结
- 眼镜,回头,去不去?
- OpenCV010:以左右摄像头的拍摄图片为基础生成深度图
- 设计模式-外观模式