When Combobox SelectedValue of property of first item is null following items property is set to null

scott thomson 66 Reputation points
2021-02-13T02:16:36.94+00:00

I am looking for some advice on how to address an issue I have due I think to nullable int type used for SelectedValue binding on a combobox. What I observe is that if the property (which is derived from an object in ViewModel that is populated from a SQL database) whose property is bound to SelectedValue is null (and is not an item in the comobox's ItemsSource) then when the xaml first loads, the combobox does not show any selected value which is I guess all it can do and thats OK, however when the user then selects a new object which does have a valid integer value for that property this value is changed (by the combobox binding it appears) to the previous objects null value. I have inserted a debug converter to trigger the debugger into this control and observed that when the user changes to the new object the Convert object value is correct but then I see the ConvertBack code is triggered and the object value then shows null and the new objects value is changed to null.

I had assumed that when the underlying object whose property is bound to the SelectedValue is changed then the combobox would receive the new items value for that property however instead I'm seeing the new items property value being changed to null.

Andy, somethings going on I dont understand.

I have sort of got a workaround although unsatisfactory to go to the users by saving the object value in a converter and changing the value back in the convertback step to the saved correct value. This means that first time the next object is selected they see null in the combobox but subsequent changes of the object then shows correct property value.

I'll try to insert code from the main pieces involved. I am using MVVM pattern, INotifyPropertyChanged etc

This is the object class that contains the property that is bound to the combobox's SelectedValue. The property is called IDOccupExp which is type int? in this class

        private Assessment _assessments;
        public Assessment Assessments
        {
            get
            {
                return _assessments;
            }
            set
            {
                _assessments = value;
                OnPropertyChanged("Assessments");
            }
        }
.
Below is the List called "OccupExps" bound to the combobox's ItemsSource for which IDOccupExp is the parent key column

private void FillOccupExps()
        {
            List<OccupExp> q = (from a in db.OccupExps
                                orderby a.OccupExpScore ascending
                                select a).ToList();
            _occupexps = new List<OccupExp>(q);
        }
        private List<OccupExp> _occupexps;
        public List<OccupExp> OccupExps
        {
            get
            {
                return _occupexps;
            }
            set
            {
                _occupexps = value;
                OnPropertyChanged("OccupExps");
            }
        }
Here is the xaml (a UserControl) showing the Grid where the DataContext (which is the object from the ViewModel) is attached and then the offending combobox showing the SelectedValue binding of IDOccupExp property.

<Grid x:Name="gridSemiQuant" DataContext="{Binding Assessments, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="822" Background="#FFE0EFF3" Height="423">
            <Grid.ColumnDefinitions>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
            </Grid.RowDefinitions>
            <ComboBox x:Name="occupExpComboBox" Grid.Column="1" Margin="0,20,0,0" SelectedValuePath="IDOccupExp" Grid.ColumnSpan="4"
                                      SelectedValue="{Binding IDOccupExp, Mode=TwoWay}" HorizontalAlignment="Left" ItemsSource="{Binding DataContext.OccupExps,Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                      Grid.Row="2" VerticalAlignment="Center" Width="580" Style="{StaticResource ComboBoxFlatStyle}"
                      IsEnabled= "{Binding DataContext.SetOccupExpState,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                      SelectedItem="{Binding DataContext.SelectedOccupExp, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid Background="Transparent" SnapsToDevicePixels="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition></ColumnDefinition>
                                <ColumnDefinition></ColumnDefinition>
                                <ColumnDefinition></ColumnDefinition>
                                <ColumnDefinition></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding OccExpPPM}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" Grid.Column="0" FontSize="14"/>
                            <TextBlock Text="{Binding OccExpmgm3}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" Grid.Column="1" FontSize="14"  />
                            <TextBlock Text="{Binding LC50}" Grid.Column="2" VerticalAlignment="Center"  HorizontalAlignment="Left" Width="150" FontSize="14" />
                            <TextBlock Text="{Binding LD50}" Grid.Column="3" VerticalAlignment="Center"  HorizontalAlignment="Left" Width="150" FontSize="14" />
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding DataContext.GetSemiQERCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,795 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. scott thomson 66 Reputation points
    2021-02-13T08:10:41.177+00:00

    I believe I have worked out what was wrong. I think I was creating multiple instances of the ViewModel and DataContext. At least changing my code to stop what I think was creating multiples has fixed the issue

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.