开发者

WPF - Display Grid of Results With Dynamic Columns/Rows

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.

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 va开发者_如何学运维lues for an individual cell


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.

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.

Binding

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

Code behind

    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++;
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜