Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I have following code in xaml:
XML
<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<doctypeaccess>).
The ComboBox in the datagrid has a list of objects (List<usergroup>).

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
SQL
public class DocTypeAccess
{
    public UserGroup UserGroup { get; set; }
}


Binding Property on ViewModel
C#
public List<DocTypeAccess> AccessList
{
    get { return m_listAccess; }
    set
    {
        m_listAccess = value;
        OnPropertyChanged("AccessList");
    }
}


What make i wrong?

Thanks for your help...
Posted
Comments
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

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

XML
<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]
 
Share this answer
 
v2
Comments
Wayne Gaylard 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900