Binding DataGridComboBoxColumn to a one to many entity framework relation

Posted by Cristian on Stack Overflow See other posts from Stack Overflow or by Cristian
Published on 2010-05-10T07:27:32Z Indexed on 2010/05/10 7:34 UTC
Read the original article Hit count: 505

Filed under:
|
|
|

I have two tables in the model, one table contains entries related to the other table in a one to many relations, for example:

Table User
  ID
  Name

Table Comments
  ID
  UserID
  Title
  Text

I want to show a datagrid in a WPF window with two columns, one text column with the User name and another column with a combobox showing all the comments made by the user.

The datagrid definition is like this:

        <DataGrid AutoGenerateColumns="False" [layout options...] Name="dataGrid1" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
            <DataGridComboBoxColumn Header="Comments"
                SelectedValueBinding="{Binding Path=UserID}"
                SelectedValuePath="ID"
                DisplayMemberPath="Title"
                ItemsSource="{Binding Path=Comments}"
                />
        </DataGrid.Columns>
    </DataGrid>

in the code I assign the DataContext like this:

dataGrid1.DataContext = entities.Users;

The entity User has a property named Comments that leads to all the comments made by the user. The queries are returning data and the user names are shown but the combobox is not being filled.

May be the approach is totally wrong or I'm just missing a very simple point here, I'm opened to learn better methods to do this.

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf-binding