Reputation: 113
I have a
ObservableCollection<CustomObj> DataInfo
in my MVVM WPF project. The CustomObj class look like this:
public class class1 : ObservableObject
{
public class1()
{
MySecondProperty = new Class2();
}
public string MyStringValue { get; set; }
public Class2 MySecondProperty { get; set; }
public List<Class3> MyThirdProperty{ get; set; }
}
When ever I bind the WPF property like this
<DataGrid Name="dgMyDataGrid"
SelectedItem="{Binding SelectedItem}"
ItemsSource="{Binding DataInfo}">
</DataGrid>
I get the value from "MyStringValue", and object and a collection in my datagrid. Google gives me no result and I can't find anything similar to this example.
How can I get my data from Class2 and from the List in a easy way to show the data?
Upvotes: 1
Views: 2230
Reputation: 7719
you need to define the columns and bind inside the column definition.
The following will show the value of MySecondProperty.SubProperty in the second column
For Class3, if you for want something like a combobox, then use a templated datagrid column http://blogs.msdn.com/b/vinsibal/archive/2008/08/19/wpf-datagrid-stock-and-template-columns.aspx has info on column templates
<DataGrid Name="dgMyDataGrid" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding DataInfo}">
<DataGrid.Columns>
<DataGridTextColumn Header="MyStringValue " Width="*" Binding="{Binding Path=MyStringValue }" />
<DataGridTextColumn Header="MySecondProperty.SubProperty" Width="*" Binding="{Binding Path=MySecondProperty.SubProperty}" />
</DataGrid.Columns>
</DataGrid>
Upvotes: 2