C# – WPF, MVVM datagrid row binding

bindingcmvvmwpf

I am wandering if anyone could help me work out the binding issues I am having?

Snippets of Code:

<DataGrid AutoGenerateColumns="False" Grid.Column="1" Grid.Row="1" SelectionMode="Single" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding PersonList}" CanUserSortColumns="True" SelectedItem="{Binding Path=SelectedPerson}" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="FirstName" Width="100" Binding="{Binding FirstName}" />
                <DataGridTextColumn Header="LastName" Width="100" Binding="{Binding LastName}" />
                <DataGridTemplateColumn Width="140" Header="Operator">
                    <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=OperatorList}" DisplayMemberPath="FullName" SelectedValue="{Binding Path=SelectedOperator}" />
                            </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn> 
                ...
</DataGrid>

Above is a snippet of code from the view:

Each person in the list is its own viewmodel that has the code snippet below:

CollectionView _operatorList;
public CollectionView DebtorAgentList { get { return _operatorList; } }

Model.Operator _selectedOperator;
public Model.Operator SelectedOperator
{...}

Now the problem I am having is that the SelectedValue binding isn't working and I can't work out why? But what makes it tricky or different (maybe) is that every row in the data grid has its own viewmodel , so in otherwords a datagrid of viewmodels. So what is happening is that FirstName and LastName and Combobox are all filled correctly but I can't seem to get the SelectedValue to bind?
P.S. It isnt because of some spell mistake, if there are spelling mistakes is because i renamed methods when I wrote the question etc.

Further Details:

The above Datagrid is part of a view that has its own viewmodel, this view model fills the datagrid above with a list of people, each person is a viewmodel in essence( well it isn't really a viewmodel but then again its more of a viewmodel than a plane model). It works the way I need it to until I try to bind the SelectedView attribute?

Could someone please tell me why that binding might not working?

Thanks In advance 😀

Best Answer

Use SelectedValuePath and then use 'SelectedValue' to select any item in the collection, see the following code :-

<ComboBox ItemsSource="{Binding Path=OperatorList}" DisplayMemberPath="FullName" SelectedValuePath="SelectedOperator" SelectedValue="{Binding SelectedOperator}" />   
Related Topic