如何使用WPF显示真正的表格数据? [英] How to display real tabular data with WPF?

查看:423
本文介绍了如何使用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显示真正的表格数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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