Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF combobox DataGrid
Hello
 
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">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Group">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox
                        SelectedItem="{Binding Path=UserGroup, Mode=TwoWay}"
                        ItemsSource="{Binding Path=DataContext.UserGroupList,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                        IsReadOnly="False" Background="White" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns
 
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; }
    set
    {
        m_listAccess = value;
        OnPropertyChanged("AccessList");
    }
}
 
What make i wrong?
 
Thanks for your help...
Posted 9-Nov-11 0:57am
Comments
Wayne Gaylard at 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
good
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
 
<DataTemplate>
                    <ComboBox
                        SelectedItem="{Binding Path=UserGroup, Mode=TwoWay}"
                        SelectedValue="{Binding SelectedDocTypeAcces.UserGroup.UserGroupID}"
                        SelectedValuePath="UserGroupID"
                        ItemsSource="{Binding Path=DataContext.UserGroupList,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                        IsReadOnly="False" Background="White" />
                </DataTemplate>
 
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]
  Permalink  
v2
Comments
Wayne Gaylard at 9-Nov-11 6:50am
   
removed reply from answer to comments and deleted answer
Hello
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 at 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 515
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 155
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 9 Nov 2011
Copyright © CodeProject, 1999-2014
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