如何构建列数未知的 WPF Datagrid [英] How to build A WPF Datagrid with an unknown number of columns

查看:25
本文介绍了如何构建列数未知的 WPF Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我从 txt 获取的字符串数组集合中构建和显示 WPF 数据网格.问题是我不知道先验的列数,即单个数组中的项目数.所以我在我的 xaml <DataGrid Grid.Row="2" ItemsSource="{Binding Path=Rows}"/>

中定义

我试图在我的视图模型中填充它,但我不能简单地将我的数组集合(可观察集合)作为项目源,因为数据网格将只显示空行.

我也可以在 Observable 集合上使用其他方法,因为我用相同的方法生成数组

这是我的 Observable 集合:

ObservableCollectionRows = new ObservableCollection;

在这种方法中,我填充了集合

foreach(wsettings.lista 中的 ListViewItem 项){TextBlock line = item.Content as TextBlock;字符串 txt = line.Text;string[] x = txt.Split(stringSeparators, StringSplitOptions.None);Rows.Add(x);}

请不要介意拆分前的第一部分.我从我之前使用的文本块列表视图中获取我的数据(我有我的理由).

使代码更具可读性

标题必须是用户必须设置的组合框

解决方案

I need to build and display a WPF data grid from a collection of string array that i got from a txt. The problem is that i don't know a priori which will be the number of columns i.e. the number of item in the single array. So i defined in my xaml <DataGrid Grid.Row="2" ItemsSource="{Binding Path=Rows}" />

I was trying to fill it in my View Model, but i cannot simply put my collection (Observable Collection) of array as item source, since the datagrid will display only blank rows.

I can also use other approach over the Observable collection since i produce my array in the same method

this is my Observable Collection:

ObservableCollection<string[]> Rows = new ObservableCollection<string[]>;

in this method i fill the collection

foreach(ListViewItem item in wsettings.lista)
        {                 
            TextBlock line = item.Content as TextBlock;
            string txt = line.Text;
            string[] x = txt.Split(stringSeparators, StringSplitOptions.None);               
            Rows.Add(x);
        }    

Please don't mind the first part before the split. I take my data from a listview of text block that i used before(I have my reason).

EDIT1: made the code more readable

EDIT2: the header must be a combobox that a user must set

解决方案

There is DataTable class in .Net. Its primary purpose is to communicate with relational database but it can be used nicely to store, display and edit tabular data (e.g. read and display .csv/Excel files -> DataTable + DataGrid in wpf, DataTable + DataGridView in WinForms).

DataTable columns (DataColumn) can be added/removed at runtime and DataGrid auto-generates columns (DataGridColumn) for them (enabled by default), using Name property for headers. Also DataTable supports sorting and filtering out-of-box.

note: DataGrid doesn't clear Columns when new ItemsSource is assigned. So it is possible to have some predefined columns and use autogenerate as well.

note: DataGrid creates DataGridTextColumns by default. If more complex template is required, the process can be intercepted via AutoGeneratingColumn event (see example)

here is an example:

code

public class MyViewModel
{
    public DataTable Test { get; set; }
}

public MyWindow()
{
    InitializeComponent();
    var vm = new MyViewModel
                {
                    Test = new DataTable
                        {
                            Columns = {"A", "B", "C"}
                        }
                };            
    this.DataContext = vm;

}

xaml

<DataGrid AutoGenerateColumns="True"
          ItemsSource="{Binding Path=Test.DefaultView}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="#"/>
    </DataGrid.Columns>
</DataGrid>

the result (I entered some values)

这篇关于如何构建列数未知的 WPF Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆