Curtis
Curtis

Reputation: 5892

Binding not updating in ComboBox Text field when underlying ComboBox item description changes

I am having a binding update problem with my ComboBox. My ComboBox's ItemSource is bound to a list of LaneConfigurations. The ComboBox's SelectedItem is bound to the SelectedLaneConfiguration property in my code-behind. I configured the ComboBox's ItemTemplate to display the 'DisplayID' property for each LaneConfiguration.

This works most of the time. However, changing a lane configuration can result in the DisplayID changing. If you have a particular lane selected and it's DisplayID is being displayed as the 'Text' of the ComboBox and then you change the LaneConfiguration object, the 'Text' portion of the ComboBox is not updated with the new 'DisplayID' that should be showing. When I click the dropdown arrow on the ComboBox, the item appearing as the selected item in the list is showing the correct DisplayID, but that doesn't match the 'DisplayID' that is being shown at the top of the ComboBox in it's 'Text' field.

In my code behind, I am firing a PropertyChanged event on the 'SelectedLaneConfiguration' property. How do I get the ComboBox to realize that the 'DisplayID' property needs updating? I have tried to fire a PropertyChanged event for the 'DisplayID' property, but it is part of the LaneConfiguration class, so it doesn't appear to be updating.

I have included the XAML below along with a screenshot that shows the ComboBox Text display out of sync with the content of the ComboBox dropdown.

ScreenShot

Partial XAML:

<ComboBox x:Name="_comboBoxLanes" Height="26"
          ItemsSource="{Binding SensorConfiguration.LaneConfigurations}" 
          SelectedItem="{Binding Path=SelectedLaneConfiguration}">
     <ComboBox.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding DisplayID, Mode=OneWay}"/>
        </DataTemplate>
     </ComboBox.ItemTemplate>
</ComboBox>

Some of the code-behind:

    public partial class LaneManagement : INotifyPropertyChanged, IDisposable
    {
        ..
        ..

        public event PropertyChangedEventHandler PropertyChanged;

        private void FirePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

        private void SensorConfiguration_Changed()
        {
            LaneTools.ResetLanePolygons();
            GenerateLaneTypeDropdowns();
            FirePropertyChanged("SensorConfiguration");
            FirePropertyChanged("SelectedLaneConfiguration");
            FirePropertyChanged("DisplayID");
        }
   }


   public class LaneConfiguration : PolygonConfiguration
   {
       public override string DisplayID
       {
          get
          {
              return IsLaneGroup?string.Format("Lanes {0} - {1}", ID, ID + LaneCount - 1):                                  string.Format("Lane {0}", ID);
          }
       }
   }

Upvotes: 1

Views: 3887

Answers (2)

Artem Makarov
Artem Makarov

Reputation: 874

The right way is to make properties that you bindin-g - DependencyProperty - than you'll not have any problems with auto update.

If you use INotifyPropertyCahgnge than it must work, if it don't - check if PropertyChanged event == null.

Upvotes: 0

Curtis
Curtis

Reputation: 5892

I created a shorter example of this and reposted and got an answer.

I thought I only needed INotifyPropertyChanged on the code-behind. I actually needed it in my data object LaneConfiguration class as well. I should have paid closer attention to Ali Adl's comment. It would have saved me a day of frustration..

Thanks Ali Adl and Tim for your helpful answers !!

Upvotes: 1

Related Questions