1

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?

2
  • What would you like your Class2 to be displayed as? Commented Dec 12, 2011 at 16:10
  • I was thinking of a way that Class2 have other properties that I would like to show as columns. And generate columns "on-the-fly" depending on what properties each class (2&3) have. Commented Dec 13, 2011 at 7:31

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply Anton, I have looked around a little but it seams that just like your answer it is very difficult to achieve what I want. I need to generate the columns from code-behind. But I have solved my problem by creating a DataTable and bind that DataView to the DataGrid. It is not the optimal way but it prints out what I want. But thanks to your reply I know how to use SubProperty now. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.