Reputation: 159
i'm learning wpf and binding and all and i have a gridview and a custom object
i'm trying to bind a list of custom objects to the grid my custom object is designed like this
Public class myObject
{
protected int myInt {get; set;}
protected ObservableCollection<string> myStrings{get;set;}
protected double myDouble{get;set}
public myObject(int aInt, string aString, double aDouble)
{
myStrings = new ObservableCollection<string>();
string[] substrings = aString.split('/');
this.myInt = aInt;
foreach (string s in substrings)
{
myStrings.Add(s);
}
this.myDouble = aDouble;
}
}
so i then create an observablecollection of these objects and bind them to the grid
the double, int are displaying just fine in the grid but the array is displaying the pointer, i have a column filled with
"System.Collections.ObjectModel.ObservableCollection `1[System.String]"
Could anyone help me displaying the content of the observableCollection in the grid like, each item of the collection would get a column.
Thanks by advance !
SOLUTION I FOUND
I tried using templates, but it didn't please me so i used ExpandoObjects I first created a list of dictionaries containing each row of my future grid, then turned them into expando objects using https://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/ Big thanks to him for his awesome custom method
then i just bound the observable collection of ExpandoObjects to my radgridview and TADA i now have my dynamic grid with dynamic objects
Thanks again for your answers i learnt some usefull informations on templates !
Upvotes: 0
Views: 1600
Reputation: 9944
What looks more appropriate in you case is to use RowDetailsTemplate
to define a child DataGrid
/GridView
to display the Strings collection, displaying the strings collection at the same level as the other properties could be a hard task to do (and doesn't make much sense).
Here a sample of how to define a DataGrid
within another one (same thing using GridView
/ListView
, DataGrid
looks more appropriate).
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="MyInt" Binding="{Binding MyInt}"/>
<DataGridTextColumn Header="MyDouble" Binding="{Binding MyDouble}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding MyStrings}"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
MyObject
public class MyObject
{
public int MyInt { get; set; }
public ObservableCollection<string> MyStrings { get; set; }
public double MyDouble { get; set; }
public MyObject(int aInt, string aString, double aDouble)
{
MyStrings = new ObservableCollection<string>();
string[] substrings = aString.Split('/');
this.MyInt = aInt;
foreach (string s in substrings)
{
MyStrings.Add(s);
}
this.MyDouble = aDouble;
}
}
Upvotes: 1