You can create a class, for example. myGridCol, which represents a column and creates a collection of columns. Read the Google data feed and create columns. Then you need to add columns separately, for example. myGridCol [0], myGridCol [1] .. as the DataGridColumns in the code behind. You cannot directly link a collection of columns.
You just snap to a collection for rows that have a collection for columns.
In my case, I use GridView, but I used the same approach with DataGrid. In my case, sDocs is an ObservableCollection. SDoc has an open list of DocFields. The Fields collection is exactly the same in every sDoc, because I made sure that it is. If the Fields collection is not the same in every sDoc, she doesn't like it.
sDocs is an ItemSource for a GridView
Then in the code behind I add the columns. As I said, you cannot directly bind to a collection of columns. Note that you can even bind a property path (e.g. DispValueShort). My class for DocField has other properties and methods. DocField is actually an abstract class with the DispValueShort abstract property. Then I have classes for string, date, and enumeration that implement DocField, because editing a string is different than editing a date. I even have classes for a single value and a multi-valued value. This is a stable production application.
Snap
<ListView Grid.Row="1" Grid.Column="0" x:Name="lvSrchResulsGrid" ItemsSource="{Binding Path=MyGabeLib.Search.SDocs}"
Code for
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++; }
source share