WPF - Display Grid of Results With Dynamic Columns/Rows

后端 未结 1 731
礼貌的吻别
礼貌的吻别 2020-12-04 00:27

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 bee

相关标签:
1条回答
  • 2020-12-04 01:19

    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 讨论(0)
提交回复
热议问题