130

I have got a combo box with items source attached using simple binding. Is there any way to refresh this binding once combo box is loaded?

Mafii
  • 7,227
  • 1
  • 35
  • 55
Techee
  • 1,755
  • 5
  • 14
  • 15
  • 1
    What do you mean by simple binding? Normally when you use binding the control should automatically refresh. – Emond Apr 15 '11 at 13:14
  • 14
    Techee, no offence, but I believe H.B. deserves his answer to be accepted ;-) – Dani May 20 '14 at 08:56
  • 2
    @Dani I'm not sure Techee is ever coming back - six and a half years since he's been logged in – The Lonely Coder Oct 05 '17 at 16:28
  • I don't really understand what is being asked. Is the entire collection being changed? Only its contents? What type of collection is being used? There is not enough detail. – StayOnTarget Apr 04 '23 at 18:18

5 Answers5

230

You can use binding expressions:

private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
    ((ComboBox)sender).GetBindingExpression(ComboBox.ItemsSourceProperty)
                      .UpdateTarget();
}

But as Blindmeis noted you can also fire change notifications, further if your collection implements INotifyCollectionChanged (for example implemented in the ObservableCollection<T>) it will synchronize so you do not need to do any of this.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Doesn't seem to do a thing for me using a ListBox. – Jonathan Wood Jul 04 '16 at 14:57
  • 1
    @JonathanWood: Well, i cannot divine what kind of code you have, including what your binding looks like. Does the binding even work in the first place? – H.B. Jul 04 '16 at 15:31
62

if you use mvvm and your itemssource is located in your vm. just call INotifyPropertyChanged for your collection property when you want to refresh.

OnPropertyChanged(nameof(YourCollectionProperty));
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • 11
    This is the cleanest approach imho. – Boyan Oct 13 '15 at 12:21
  • 1
    This should be done where possible, but it should be noted it's not always practical. For instance if you're binding to a serial port, and want to check whether it's open, closed, the baud rate, etc you can create a wrapper class around the serial port that implements `INotifyPropertyChanged`, but you will have to keep the port private to that wrapper and thus need to write a property and method for everything on that port you use elsewhere in the project to ensure that the properties you are interested in notifying on always go through the wrapper – Assimilater Aug 18 '17 at 18:18
  • Point to Note : This will not update the UI when there is an item added or removed from list. For this scenario you have to use observable collection – Rankit Dua Mar 21 '23 at 06:58
  • if you call OnPropertyChanged(nameof(YourCollectionProperty)); after adding or removing an item - it works. but you are right observable collection to this build in. – blindmeis Mar 24 '23 at 08:59
41

To add my 2 cents, if you want to update your data source with the new value of your Control, you need to call UpdateSource() instead of UpdateTarget():

((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
dotNET
  • 33,414
  • 24
  • 162
  • 251
10

MultiBinding friendly version...

private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
    BindingOperations.GetBindingExpressionBase((ComboBox)sender, ComboBox.ItemsSourceProperty).UpdateTarget();
}
Egemen Çiftci
  • 699
  • 5
  • 13
6

Try using BindingExpression.UpdateTarget()

Machavity
  • 30,841
  • 27
  • 92
  • 100
Kushal Waikar
  • 2,976
  • 5
  • 26
  • 31