Itemsource a two-dimensional string array as a WPF listview

Source: Internet
Author: User
Tags bind xmlns

In WPF, we usually use bindings, an array of objects as a data source, and objects have many properties, indirectly equivalent to a two-dimensional array, but to do so we have to know in advance what the object has properties, there are several properties to bind, in case we are not sure how many rows of the ListView column, Needs to be determined at run time. Examples are as follows:
First Picture:

Second Picture:

The two graphs have different columns of data, and there may be other file data with different columns, and the requirement now is that our WPF ListView can load the file and then display it.
Because there is uncertainty, the model cannot be defined beforehand to bind to the ListView. If the listview of WinForm can be a column-by-column assignment, how does WPF implement it? Please see:
First we want to get the column name: A string array is used to represent the data with a two-dimensional string array:

string[] headers = new string[] {"Name", "Age", "height"};
string[][] datas = new string[][]
 {
     new string[] {"111", "1", "165"},
     new string[] {"222", "2", "165"}
 };

Note that the length of the headers is the same as the length of the second dimension of Datas.
This example foreground code:

<window x:class= "Two-dimensional array binding 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: two-dimensional array binding listview "
        mc:ignorable=" D "
        title=" MainWindow "height=" width= "525" loaded= "window_loaded" >
    <Grid>
        <listview name= "LV" > </ListView>
    </Grid>
</Window>

Dynamically create columns for the ListView (depending on the length of the 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];//Sets the column name
                //here sets the binding of the array, the array takes the No. 0 element to list[0], and takes the column by the indexer
                . displaymemberbinding = new Binding (string. Format ("[{0}]", i));
                GRIDVIEW.COLUMNS.ADD (column);
            }
        }

One of the most important sentences is:column. displaymemberbinding = new Binding (string. Format ("[{0}]", i)); Dynamically binding elements in an array of strings
All foreground code is posted as follows:

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 two-dimensional array binding 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 Strin
            G[] {"Name", "Age", "height"}; string[][] datas = new string[][] {new string[] {"111", "1", "165"}, New Strin
            G[] {"222", "2", "165"}};
   Production of column generatecolumns (LV, headers) According to the length of the header;         Sets the data source LV.

        ItemsSource = datas; } private void Generatecolumns (ListView listview, String[] headers) {if (headers = = NULL | | h Eaders.
            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];//Sets the column name//set the array's bindings here, the array takes the No. 0 element as list[0], and takes the column by the indexer. displaymemberbinding = new Binding (string.
                Format ("[{0}]", i));
            GRIDVIEW.COLUMNS.ADD (column);
 }
        }
    }
}

Operation Result:

Add Column to Header:

private void Window_Loaded (object sender, RoutedEventArgs e)
        {
            //string[] headers = new string[] {"Name", "Age", "height " };
            string[][] datas = new string[][]
            //{
            //    new string[] {"111", "1", "165"},
            //    new string[] {"222", "2", "165"}
            //};
            string[] headers = new string[] {"Name", "Age", "height", "weight", "education", "domicile"};
            string[][] datas = new string[][]
            {
                new string[] {"111", "1", "165", "45KG", "undergraduate", "Hubei"},
                new string[] {"222 "," 2 "," 165 "," 45KG "," College "," Hunan "}
            ;
            Production of column
            generatecolumns (LV, headers) According to the length of the header;
            Sets the data source
            LV. ItemsSource = datas;

        }

The results of the operation are as follows:

this solves the problem of using the ListView to dynamically display and bind the data when the column is indeterminate.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.