Click here to Skip to main content
11,921,353 members (64,349 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF combobox DataGrid

I have following code in xaml:
<DataGrid AutoGenerateColumns="False" HorizontalGridLinesBrush="Black" IsReadOnly="False"
          ItemsSource="{Binding AccessList, Mode=TwoWay}" Margin="5,8,5,0" Name="dgAccess">
        <DataGridTemplateColumn Header="Group">
                        SelectedItem="{Binding Path=UserGroup, Mode=TwoWay}"
                        ItemsSource="{Binding Path=DataContext.UserGroupList,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                        IsReadOnly="False" Background="White" />

The grid has a list of objects (List).
The ComboBox in the datagrid has a list of objects (List).

When i select a group in a row, the binding sould set the property UserGroup in my object AccessList. But it not works. The AccessList.UserGroup is always null?

Class DocTypeAccess
public class DocTypeAccess
    public UserGroup UserGroup { get; set; }

Binding Property on ViewModel
public List<DocTypeAccess> AccessList
    get { return m_listAccess; }
        m_listAccess = value;

What make i wrong?

Thanks for your help...
Posted 9-Nov-11 0:57am
Wayne Gaylard 9-Nov-11 6:54am
Please see my comments below. I deleted your 'answer' and set it as a commetn to my answer.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You don't give the properties for your UserGroup class, but if you want to bind the SelectedItem you need to identify a property in the UserGroup class that is unique to each UserGroup and then you need to bind the SelectedValue property and the SelectedValuePath to this property. So, just say your UserGroup class had a property called UserGroupID then you would set the SelectedValue property like this

                        SelectedItem="{Binding Path=UserGroup, Mode=TwoWay}"
                        SelectedValue="{Binding SelectedDocTypeAcces.UserGroup.UserGroupID}"
                        ItemsSource="{Binding Path=DataContext.UserGroupList,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                        IsReadOnly="False" Background="White" />

I hope this makes sense. If not please post your UserGroup class code and We can work from there.

[Edit] By the way, this assumes you have a property in your ViewModel that returns an instance of a DocAccessType class called SelectedDocAccessType, which is bound to your DataGrid's SelectedItem property.[/Edit]
Wayne Gaylard 9-Nov-11 6:50am
removed reply from answer to comments and deleted answer
Thanks for your answer. I have change the code but it not works. Then i make a new string property "test" in the DocAccessType class. I add a column to the Datagrid that response to the new property "test". So i can check if the problem is the combobox or not.
So i test it:
when i select a group in the combo and save: don't work
when i select a group and then change the second column "test": work, i have the group and the test...?

Must i have a trigger for refresh or what's the problem?
Wayne Gaylard 9-Nov-11 6:53am
You should really make your collections ObservableCollections rather than Lists, as they Implement INotifycCollectionChanged automatically. I suggest you post your viewmodel code so I can have a look - edit your original question and post it there. Please do not post questions as answers - use the comments rather.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 553
1 Dave Kreskowiak 245
2 phil.o 160
3 KrunalRohit 134
4 Sergey Alexandrovich Kryukov 132
0 OriginalGriff 6,951
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,171
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web02 | 2.8.151120.1 | Last Updated 9 Nov 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100