0
votes

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 :D

2
SelectedMode binding mode need to be two way? Are you firing property changed events? - Derek Beattie
I tried setting the selectedMode, but that didn't do anything (isn't twoway by default), yea it should be firing property changed events, but Ill try force it in places and see what happens :) - Heinrich
Mode is OneWay by default. Your code didn't show any implementation of INotifyPropertyChanged so I wasn't sure. - Derek Beattie
Oh true thanks, I worked it out now but thanks - Heinrich

2 Answers

0
votes

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}" />   
0
votes

I found this article that helped me work it out :) instead of using a template column I used the DataGridComboBoxColumn, as below:

<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}" />
            <DataGridComboBoxColumn Header="Operator" DisplayMemberPath="FullName" Width="150" SelectedValueBinding="{Binding Path=SelectedOperator}" >
                <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding Path=OperatorList}" />
                </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Path=OperatorList}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
            ...
</DataGrid>

The rest stayed the same, Thanks all :)