如何使用WPF显示真正的表格数据?

编程入门 行业动态 更新时间:2024-10-28 08:19:13
本文介绍了如何使用WPF显示真正的表格数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下问题:我必须在某种网格中显示表格数据。

说A类是一个数据项,其属性为Foo和Bar,并且我有一个列表List或者像这样的项目。我想在网格中显示这些项目,其中具有相同Foo的项目位于一列中,并且具有相同Bar的项目位于一行中。基本。简单。我想呢?我最初的尝试是创建一个ItemsControl,使用一个Grid作为ItemsPanelTemplate,以及一个ItemsTemplate,其中我绑定了Grid.Row和Grid.Column附属属性A.Foo和A.Bar,使用IValueConverters将特定的Foo与特定的列相关联,例如转换器将在一列Columns数组中查找Foo值并返回正确的索引。 问题是,为Grid创建行和列定义。由于它们是完全动态的(我不知道在设计时,Foo's和Bar's会在源集合中),所以我无法在xaml文件中定义它们。但是在运行时,ItemsControl实际上并不提供对当前ItemsPanel实例的访问。 我发现的唯一方法是使用VisualTreeHelper.GetChild的一种肮脏的方式,并且我设法得到了ItemsPanel实例并创建了所需的Row-和ColumnDefinitions,但是我的ItemTemplate-d项目不会呈现在右侧行和列,虽然我的转换器返回了右列/行索引。也许我并不真正了解ItemsControl如何在内部工作,但事情是,我的想法没有了。

有人建议将ListView与GridView一起使用,将我的条目分组到另一个对象中,这会使我无法动态创建行,但动态创建列看起来颇为流畅复杂。

有人有想法或至少有提示吗?我错过了什么明显的东西?

解决方案

因为您提到切换到ListView的潜力,我认为我会建议使用DataGrid,因为一旦加载数据到DataTable中,DataGrid会为您生成所有列和行来完成所有工作。

以下是我复制的一些工作代码,以便您开始使用。

以下是DataGrid的绑定:

< WpfToolkit:DataGrid IsReadOnly =True IsTabStop =False ItemsSource ={Binding Path = GridData,Mode = OneWay}> < / WpfToolkit:DataGrid>

定义类中XAML绑定的属性(我使用MVVM模式):

private DataTable _dt = new DataTable(MyDataTable); public DataView GridData { get { return _dt.DefaultView; $ b

将DataColumns添加到DataTable中,如下所示: p>

DataColumn fileType = new DataColumn(FileType); fileType.AllowDBNull = true; fileType.DataType = typeof(string); _dt.Columns.Add(fileType);

将DataRows添加到您的DataTable中,如下所示:

DataRow dr = _dt.NewRow(); dr [FileType] =* .txt; _dt.Rows.Add(dr);

重置行和列如下:

_dt.Rows.Clear(); _dt.Columns.Clear();

使用这些部分是我动态生成DataGrid所需的全部内容。

i've got following problem: i have to display tabular data in some kind of grid.

Say class A is a data item, with properties Foo and Bar, and i have a list of items List or sth like this. i want to display these items in a grid, where items with same Foo are in one column, and items with same Bar in one row. basic. simple. i thought?

My initial attempt was to create an ItemsControl, use a Grid as ItemsPanelTemplate, and an ItemsTemplate where i bound the Grid.Row and Grid.Column attached properties to A.Foo and A.Bar, using IValueConverters to associate a specific Foo with a specific Column, e.g. the converter would look up the Foo value in a kind of Columns array and return the proper index. The thing is, building the row and column defintions for Grid. As they are completely dynamic (i dont know at design time, which Foo's and Bar's will be in the source collection), i cannot define them in the xaml file. But at runtime, ItemsControl really doesnt provide access to the current ItemsPanel instance. The only way i found was a dirty way using VisualTreeHelper.GetChild, and i managed to get the ItemsPanel instance and create the needed Row- and ColumnDefinitions, but my ItemTemplate-d items just wouldn't be rendered in the right row and column, although my Converters returned the right column/row indices. Maybe i don't really understand how ItemsControl works internally, but the thing is, im out of ideas.

Someone suggested using ListView with a GridView, grouping my items of same Bar in another object, this would relieve me of creating the rows dynamicly, but creating the columns dynamicly seems quite complicated.

Does anyone have an idea or at least a hint? Did i miss something obvious?

解决方案

Because you mention the potential of switching to a ListView I thought I would recommend using a DataGrid instead because once you load your data into a DataTable the DataGrid does all the work for you to generate the columns and rows.

Here is some working code I copied to get you started.

Here is the binding for the DataGrid:

<WpfToolkit:DataGrid IsReadOnly="True" IsTabStop="False" ItemsSource="{Binding Path=GridData, Mode=OneWay}"> </WpfToolkit:DataGrid>

Define a property for the XAML binding in a class (I use the MVVM pattern):

private DataTable _dt = new DataTable("MyDataTable"); public DataView GridData { get { return _dt.DefaultView; } }

Add the DataColumns to your DataTable as follows:

DataColumn fileType = new DataColumn("FileType"); fileType.AllowDBNull = true; fileType.DataType = typeof(string); _dt.Columns.Add(fileType);

Add the DataRows to your DataTable as follows:

DataRow dr = _dt.NewRow(); dr["FileType"] = "*.txt"; _dt.Rows.Add(dr);

Reset the rows and columns as follows:

_dt.Rows.Clear(); _dt.Columns.Clear();

Using these parts is all I needed to dynamically generate a DataGrid.

更多推荐

如何使用WPF显示真正的表格数据?

本文发布于:2023-11-07 01:21:15,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   表格   数据   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!