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.
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("YourCollectionProperty");
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();
MultiBinding friendly version...
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
BindingOperations.GetBindingExpressionBase((ComboBox)sender, ComboBox.ItemsSourceProperty).UpdateTarget();
}
Try using BindingExpression.UpdateTarget()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With