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?
-
1What do you mean by simple binding? Normally when you use binding the control should automatically refresh. – Emond Apr 15 '11 at 13:14
-
14Techee, 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
6 Answers
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.
-
-
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
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));
- 9,925
- 10
- 45
- 68
- 21,626
- 7
- 52
- 71
-
9
-
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
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();
- 31,005
- 20
- 138
- 226
MultiBinding friendly version...
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
BindingOperations.GetBindingExpressionBase((ComboBox)sender, ComboBox.ItemsSourceProperty).UpdateTarget();
}
- 356
- 2
- 8
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();
}
- 864
- 10
- 19
-
-
I tried this.Bindings.Update() in a usercontrol in UWP and it does not exist. – James Esh Dec 22 '16 at 01:34
-
-
2Bindings.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