121

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
  • 6,734
  • 1
  • 37
  • 54
Techee
  • 1,649
  • 4
  • 14
  • 15

6 Answers6

219

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.
  • 142,212
  • 27
  • 297
  • 366
  • 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
60

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
  • 9,925
  • 10
  • 45
  • 68
blindmeis
  • 21,626
  • 7
  • 52
  • 71
  • 9
    This is the cleanest approach imho. – Boyan Oct 13 '15 at 12:21
  • 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
37

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
  • 31,005
  • 20
  • 138
  • 226
10

MultiBinding friendly version...

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

Try using BindingExpression.UpdateTarget()

Machavity
  • 29,816
  • 26
  • 86
  • 96
Kushal Waikar
  • 2,966
  • 4
  • 24
  • 31
-1

I was fetching data from backend and updated the screen with just one line of code. It worked. Not sure, why we need to implement Interface. (windows 10, UWP)

    private void populateInCurrentScreen()
    {
        (this.FindName("Dets") as Grid).Visibility = Visibility.Visible;
        this.Bindings.Update();
    }
Itzdsp
  • 864
  • 10
  • 19
  • This.Bindings.update() refresh all the bindings in the screen. FYI – Itzdsp Dec 21 '16 at 21:43
  • I tried this.Bindings.Update() in a usercontrol in UWP and it does not exist. – James Esh Dec 22 '16 at 01:34
  • Yes. And this.Bindings still does not exist. – James Esh Dec 22 '16 at 01:54
  • 2
    Bindings.Update() is available only where the compiled bindings (x:Bind) are used... and compiled bindings are available only in UWP – Marian Dolinský Dec 22 '16 at 18:06
  • Please take a look at @MarianDolinský comment. Looks like you have to bind from xaml – Itzdsp Dec 23 '16 at 14:01
  • @JamesEsh if you are using "classic" {Binding ...} expression, then it's not available. You have to use {x:Bind ...} to be able to use Bindings.Update(); etc... Take a look here: https://msdn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-quickstart – Marian Dolinský Dec 23 '16 at 15:43
  • Except that it will show up in the intellisense whether or not I use x:bind – James Esh Dec 23 '16 at 19:31
  • It will not show up until you compile your app when using x:Bind expression, because "this.Binding" is automatically generated field. Take a look here: http://imgur.com/a/rbFFv – Marian Dolinský Dec 24 '16 at 11:20