0
votes

I am having an issue binding the selected value of a combobox back to its listview column value.

My Listview has a datacontext of Inquiries which is a collection viewsource The ComboBox as a datacontext of Accounts wich is also a collection viewsource

Both display the lists correctly by setting the itemssource to {Binding}

What I need to do is bind the Combobox selectedvalue to the Inquiries row that it sits in.

Here's the Account Column XAML:

<GridViewColumn x:Name="AccountIdColumn" Header="Account" Width="80">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Margin="-6,-1" DataContext="{StaticResource AccountViewSource}" 
                                      ItemsSource="{Binding}"
                                      x:Name="AccountComboBox"
                                      SelectedValuePath="ID" 
                                      DisplayMemberPath="Description" 
                                      Height="Auto" 
                                      SelectedValue="{Binding AccountID, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
       </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

And the Listview is set like this:

<ListView DataContext="{StaticResource tbl_InquiriesViewSource}" x:Name="Tbl_InquiriesListView" ItemsSource="{Binding Mode=OneWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" Margin="10,47,10,10" SelectionMode="Single" SourceUpdated="Tbl_InquiriesListView_SourceUpdated" TargetUpdated="Tbl_InquiriesListView_TargetUpdated" >
        <ListView.ItemContainerStyle>
            <Style>
                <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                ....

I know why it's not working, but can't figure out how to get it working.

2

2 Answers

0
votes

Don't change the DataContext of ComboBox. Just set the ItemsSource to StaticResource Try doing

           <ComboBox Margin="-6,-1"              
                                  ItemsSource="{Binding Source={StaticResource AccountViewSource}"
                                  x:Name="AccountComboBox"
                                  SelectedValuePath="ID" 
                                  DisplayMemberPath="Description" 
                                  Height="Auto" 

OR update the SelectedValue binding as

 SelectedValue="{Binding DataContext.AccountID, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}} Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
0
votes

Found the solution, well found a work around! Thanks Nit for the push in the right direction. I have put the combobox in a grid and bound the grids Tag property to the AccountId then used relative binding to get / set the tag

<Grid Tag="{Binding AccountId, Mode=TwoWay}">
    <ComboBox DataContext="{StaticResource AccountViewSource}" 
              ItemsSource="{Binding}" 
              DisplayMemberPath="Description"
              SelectedValuePath="ID"
              SelectedValue="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=Tag, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>