WPF - 使用动态列/行显示结果网格 [英] WPF - Display Grid of Results With Dynamic Columns/Rows

查看:41
本文介绍了WPF - 使用动态列/行显示结果网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询一个在线服务(谷歌数据提要),它可以返回每个请求的列数和行数不同的结果.

I'm querying an online service (google data feed) which can return results that will have different numbers of columns and rows with each request.

到目前为止,我一直无法让数据网格或网格为我工作.理想情况下,我想要像 excel 一样的东西 - 您只需添加行并为单个单元格设置值

So far I have been unable to get the data grid or grid to work for me. Ideally I want something that works like excel - you can just add rows and set the values for an individual cell

推荐答案

您可以创建一个类,例如myGridCol 表示列并创建列的集合.阅读谷歌数据提要并创建列.然后您需要单独添加列,例如myGridCol[0], myGridCol[1] .. 作为后面代码中的 DataGridColumns.您不能直接绑定到列集合.

You can create a class e.g. myGridCol that represents the column and create a collection of the columns. Read the google data feed and create the columns. Then you need to add the columns individually e.g. myGridCol[0], myGridCol[1] .. as DataGridColumns in the code behind. You cannot bind directly to a column collection.

您只需将行绑定到具有列集合的集合.
就我而言,我使用的是 GridView,但我对 DataGrid 使用了相同的方法就我而言,sDocs 是一个 ObservableCollectionsDoc 有公共列表 DocFieldsFields 集合在每个 sDoc 中完全相同,因为我确定它是.如果每个 sDoc 中的 Fields 集合不相同,那么它就不一样.
sDocs 是 GridView
的 ItemsSource然后在后面的代码中添加列.正如我之前所说,您不能直接绑定到列集合.请注意,您甚至可以将 Path 绑定到一个属性(例如 DispValueShort).我的 DocField 类有其他属性和方法.DocField 实际上是一个带有抽象属性 DispValueShort 的抽象类.然后我有实现 DocField 的字符串、日期和枚举类,因为编辑字符串与编辑日期不同.我什至有单值和多值的课程.这是一个稳定的生产应用程序.

You simply bind to a collection for the rows that has a collection for the columns.
In my case I am using a GridView but I have used the same approach with DataGrid In my case sDocs is an ObservableCollection sDoc has public List DocFields The Fields collection is exactly the same in each sDoc because I made sure it was. If the Fields collection is not the same in each sDoc then it does not like that.
sDocs is the ItemsSource for the GridView
Then in the code behind I add the columns. As I said before you cannot bind directly to a columns collection. Notice you can even bind the Path to a Property (.e.g. DispValueShort). My class for DocField has other Properties and methods. DocField is actually an Abstract Class with and Abstract Property DispValueShort. Then I have classes for string, date, and enumeration that implement DocField because edit of a string is different from edit of a date. I even have classes for single value and multi value. This is a stable production application.

绑定

    <ListView Grid.Row="1" Grid.Column="0" x:Name="lvSrchResulsGrid" 
            ItemsSource="{Binding Path=MyGabeLib.Search.SDocs}"

背后的代码

    sDocBaseResultDocsFieldsIndex = 0;      
    foreach (GabeLib.DocField docField in sDocBaseResultDocsFields)
    {
        // Debug.WriteLine("  sDocBaseResultDocsFields DispName = " + docField.FieldDef.DispName);
        if (fd.FieldDef == docField.FieldDefApplied.FieldDef)
        {
             gvc = new GridViewColumn();                 
             gvch = new GridViewColumnHeader();
             gvch.Content = fd.FieldDef.DispName;
             gvch.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
             if (fd.FieldDef.Sort)
             {
                 gvch.Click += new RoutedEventHandler(SortClick);
                 gvch.Tag = fd.FieldDef.Name;
             }

             if (!fd.AppliedDispGrid) gvc.Width = 0;  // how to hide
             gvc.Header = gvch;

             gvBinding = new Binding();
             gvBinding.Mode = BindingMode.OneWay;
             gvBinding.Path = new PropertyPath("DocFields[" + sDocBaseResultDocsFieldsIndex.ToString() + "].DispValueShort");

             template = new DataTemplate();
             textblock = new FrameworkElementFactory(typeof(TextBlock));
             textblock.SetValue(TextBlock.TextProperty, gvBinding);
             textblock.SetValue(TextBlock.TextTrimmingProperty, TextTrimming.WordEllipsis);

             // <Setter Property="TextTrimming" Value="WordEllipsis" />

             template.VisualTree = new FrameworkElementFactory(typeof(Grid));
             template.VisualTree.AppendChild(textblock);

             gvc.CellTemplate = template;

             gvSearchResults.Columns.Add(gvc);
             break;
         }
         sDocBaseResultDocsFieldsIndex++;
     }

这篇关于WPF - 使用动态列/行显示结果网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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