SchroedingersCat
SchroedingersCat

Reputation: 505

Bound TextBox does not update

I have a ComboBox bound to an ObservableCollection of objects (with several properties). The Combo Box accurately displays the desired property of all objects and I can select any item from the Combo as expected.

<ComboBox Height="23" Name="comboBox1" Width="120" Margin="5" ItemsSource="{Binding Issues}" DisplayMemberPath="Issue" SelectedValuePath="Issue" SelectedValue="{Binding Path=Issues}" IsEditable="False" SelectionChanged="comboBox1_SelectionChanged" LostFocus="comboBox1_LostFocus" KeyUp="comboBox1_KeyUp" Loaded="comboBox1_Loaded" DropDownClosed="comboBox1_DropDownClosed" IsSynchronizedWithCurrentItem="True" />

I have a series of text boxes which are supposed to display other properties of the selected object. This works fine too.

<TextBox Height="23" Name="textBox5" Width="59" IsReadOnly="True" Text="{Binding Issues/LastSale, StringFormat={}{0:N4}}" />
<TextBox Height="23" Name="textBox9" Width="90" IsReadOnly="True" Text="{Binding Path=Issues/LastUpdate, Converter={StaticResource TimeConverter}}" />

BUT... The properties of ObservableCollection are updated in the Code-Behind on a regular basis and I make a change to the OC by either adding or removing a dummy object in it every time the properties are updated. (I found this simpler than other solutions).

BUT...the data in the TextBoxes DO NOT change! :-( If I select a different object from the ComboBox I get updated info, but it does not change when the OC is changed.

The OC is composed of a bunch of these Objects:

public class IssuesItems
{
  public String Issue { get; set; }
  public Double LastSale { get; set; }
  public DateTime LastUpdate { get; set; }
  ...
 }

The OC is defined as:

public ObservableCollection<IssuesItems> Issues { get; set; }

and instantiated:

this.Issues = new ObservableCollection<IssuesItems>();

What am I doing wrong? Everything I read says that when the LastSale and LastUpdate properties are changed in the OC (and I do something to force an update of the OC) the data in the text boxes ought to change.

Upvotes: 0

Views: 706

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

ObservableCollection implements INotifyCollectionChanged which allows GUI to refresh when any item is added or deleted from collection (you need not to worry about doing it manually).

But like i mentioned this is restricted to only addition/deletion of items from collection but if you want GUI to refresh when any underlying property gets changed, your underlying source class must implement INotifyPropertyChanged to give notification to GUI that property has changed so refresh yourself.

IssuesItems should implement INPC interface in your case.

Refer to this - How to implement INotifyPropertyChanged on class.

  public class IssuesItems : INotifyPropertyChanged
  {
      private string issue;
      public string Issue
      {
          get { return issue; }
          set
          {
              if(issue != value)
              {
                 issue= value;
                 // Call OnPropertyChanged whenever the property is updated
                 OnPropertyChanged("Issue");
              }
          }
      }

      // Declare the event 
      public event PropertyChangedEventHandler PropertyChanged;
      // Create the OnPropertyChanged method to raise the event 
      protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
  }

Implement other properties just like Issue as mentioned above.

Upvotes: 1

Related Questions