0

I have an object called TestData that contains two properties, a string and an ObservableCollection. I have an ObservableCollection that I use to generate columns in a DataGrid in code. I have no problem dynamically generating the DataGridTextColumn and their respective headers based off of the TestData object, but I cannot bind the DataGridTextColumn Binding property to the ObservableCollection inside of TestData.

Now that I have stated the problem in words, let me clear it up by putting up the code:

TestData.cs

public class TestData
{
    private ObservableCollection<string> data = new ObservableCollection<string>();
    public string Header { get; set; }
    public ObservableCollection<string> Data
    {
        get
        {
            return data;
        }

        set
        {
            data = value;
        }
    }
}

MainWindow.xaml.cs Code Behind

public partial class MainWindow : Window
{
    private ViewModel viewModel = new ViewModel();
    private List<string> headers = new List<string>();
    private ObservableCollection<TestData> mainData = new ObservableCollection<TestData>();        


    public MainWindow(ViewModel vm)
    {
        InitializeComponent();
        viewModel = vm;
        this.DataContext = viewModel;     


        mainData = viewModel.MainData;

        this.spreadSheet.ItemsSource = viewModel.MainData;            

        foreach (TestData data in mainData)      
        {
            if (headers.Contains(data.Header) == false)
            {
                headers.Add(data.Header);
                DataGridTextColumn col = new DataGridTextColumn();                    
                int x = viewModel.MainData.IndexOf(data);                    
                Binding bind = new Binding("Data");                    
                bind.BindsDirectlyToSource = true;                    
                bind.Source = mainData[x];                    
                col.Binding = bind;
                col.Header = data.Header;                                      
                this.spreadSheet.Columns.Add(col);        
            }

        }
    }
}

XAML MainWindow.xaml

<Grid>                
    <DataGrid   AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch"  Name="spreadSheet" VerticalAlignment="Stretch"  Width="Auto" />

</Grid>

If I place 4 TestData objects in my ObservableCollection I get their headers fine, but all of the data in the ObservableCollection inside the TestData objects just show as (Collection) in the DataGrid.

Thanks in advance!

estarkey7
  • 63
  • 3
  • 8

1 Answers1

0

I don't think you are looking at this correctly.
You don't bind a separate collection with a single string to each column.
You bind a single two dimensional collection of rows and columns to the DataGrid as a whole.

Notice a column does not have an ItemsSource Property. Only a path Property. DataGridTextColumn

DataGrid has an ItemsSource Property. DataGrid

public class Myrow 
{
    public string Col1 { get; private set; }  
    public string Col2 { get; private set; } 
    public Myrow (string col1, string col2) { Col1 = col1, Col2 = Col2 }
}

ObservableCollection<Myrow> Myrows ...

MyRows is bound to the DataGrid (not column)

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • The issue is, I have no idea how many column of data I have to display, and I also don't know how many rows of data will be displayed. You can bind a collection to a specific column as shown in XAML here [link](http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtextcolumn(v=vs.100).aspx) but I need to dynamically bind to a collection that resides inside an object. – estarkey7 Sep 25 '12 at 17:26
  • Wrong. "Gets or sets the binding that associates the column with a property in the data source". I know how to fix this . You post a link from my answer and totally misquote. After I pointed out there is no ItemsSource Property. You are on your own. – paparazzo Sep 25 '12 at 17:51
  • Aren't they using the Binding property (in the example we both pointed to , LOL) in the DatGridTextColumn to bind to a list? – estarkey7 Sep 25 '12 at 19:27
  • But is does NOT say "bind a collection to a specific column" in that link - you are making that up. What part of "associates the column with a property" is not clear? – paparazzo Sep 25 '12 at 19:42
  • I see your point. Maybe I should look at extending DataGridTextColumn and adding support for binding to a collection. – estarkey7 Sep 26 '12 at 19:36
  • A DataGridTextColumn that binds to a collection is nothing but a DataGrid with one column. I have an answer if you are listening. http://stackoverflow.com/questions/6959600/wpf-display-grid-of-results-with-dynamic-columns-rows/6961874#6961874 – paparazzo Sep 26 '12 at 19:46
  • Thanks, your solution works. I just have to arrange my data structures a bit differently. – estarkey7 Sep 28 '12 at 16:40