Answered by:
WPF: When is data binding applied?

Question
-
Hallo,
i stuck at a behaviour i can't explain nor find any help in the documentation or various forums.
The following code is needed to explain my problem/ to ask my question. A simple WPF (VB.Net) app that has an combo box (BindingBox) that binds to a List (BindingData):
Imports System.ComponentModel Class MainWindow Implements INotifyPropertyChanged Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Protected Shadows Sub OnPropertyChanged(ByVal name As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name)) End Sub Dim _BindingData As New List(Of String) Public Property BindingData As List(Of String) Get Return _BindingData End Get Set(ByVal value As List(Of String)) _BindingData = value OnPropertyChanged("BindingData") End Set End Property Sub New() InitializeComponent() DataContext = Me BindingBox.ItemsSource = BindingData BindingData.Add("Data 1") BindingData.Add("Data 2") End Sub End Class
I added the binding to the code-behind, so i do not have to post the xaml. The behaviour is the same also if i establish the binding in the xaml.
Now to my problem/question.
The combo box BindingBox should only be updated if a new list is assigned to BindingData (source of the binding). But in this example BindingBox is also updated after i assigned BindingData to ItemsSource and added items to BindingData afterwards.
After the ui is fully loaded the combo box BindingBox is only updated if a new list is assinged to BindingData and not if items are added or removed (e.g. during a button click). Like it is described in the documentation.So why is the behaviour different during the initialization? Why are the items displayed in the combo box after the assignment to ItemsSource? I assume that it has something to do with the initialization and loading of the ui and all its element.
Monday, June 15, 2020 3:23 PM
Answers
-
Hi,
excuse me. I correct my link.--
Best Regards / Viele Grüße
Peter Fleischer (former MVP for Developer Technologies)
Homepage, Tipps, Tricks- Marked as answer by MichTUV Tuesday, June 16, 2020 2:05 PM
Tuesday, June 16, 2020 4:36 AM -
Hello,
The DataContext gets set in the Window the ListBox is on and binding is done in xaml via Binding path then in the ListBox ItemTemplate Binding, Mode = TwoWay.
Hello Karen Payne,
do you mean, due to the processing order of the binding and data context it happens that it is still possible that the maipulations of the bounded List (adding elements) is directly reflected to the combo box? So the combo box will display all changes made to the bounded list until the ui is finished loading/initializing?
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Marked as answer by MichTUV Tuesday, June 16, 2020 2:05 PM
Tuesday, June 16, 2020 11:47 AM
All replies
-
Hi
It would seem that the WPF forum has been MIGRATED to the MS Q&A site now.
Nothing wrong in asking here as it is in fact VB.NET that you are using, but, maybe post in Q&A as well.
Regards Les, Livingston, Scotland
Monday, June 15, 2020 3:36 PM -
Thank you for the advice.
Currently i am not able to add a link, but i will add it, if i will have the right to do it.Monday, June 15, 2020 4:02 PM -
Hello,
The DataContext gets set in the Window the ListBox is on and binding is done in xaml via Binding path then in the ListBox ItemTemplate Binding, Mode = TwoWay.
Here is an update class example to providing notification where you don't need to raise an event with the property name. I used JetBrains.Annotations in this example as this helps define notifications.
See this example
https://github.com/karenpayneoregon/WpfListBoxStyle/tree/master/WpfAppExample2
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
Monday, June 15, 2020 4:47 PM -
Hi,
I answer here.
--
Best Regards / Viele Grüße
Peter Fleischer (former MVP for Developer Technologies)
Homepage, Tipps, Tricks- Edited by Peter Fleischer Tuesday, June 16, 2020 4:35 AM correct link
Monday, June 15, 2020 7:18 PM -
Hello,
maybe i am missing something, but i do not see how it is related to my question?Tuesday, June 16, 2020 4:29 AM -
Hi,
excuse me. I correct my link.--
Best Regards / Viele Grüße
Peter Fleischer (former MVP for Developer Technologies)
Homepage, Tipps, Tricks- Marked as answer by MichTUV Tuesday, June 16, 2020 2:05 PM
Tuesday, June 16, 2020 4:36 AM -
Hello,
The DataContext gets set in the Window the ListBox is on and binding is done in xaml via Binding path then in the ListBox ItemTemplate Binding, Mode = TwoWay.
Hello Karen Payne,
do you mean, due to the processing order of the binding and data context it happens that it is still possible that the maipulations of the bounded List (adding elements) is directly reflected to the combo box? So the combo box will display all changes made to the bounded list until the ui is finished loading/initializing?Tuesday, June 16, 2020 4:42 AM -
Hello,
The DataContext gets set in the Window the ListBox is on and binding is done in xaml via Binding path then in the ListBox ItemTemplate Binding, Mode = TwoWay.
Hello Karen Payne,
do you mean, due to the processing order of the binding and data context it happens that it is still possible that the maipulations of the bounded List (adding elements) is directly reflected to the combo box? So the combo box will display all changes made to the bounded list until the ui is finished loading/initializing?
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Marked as answer by MichTUV Tuesday, June 16, 2020 2:05 PM
Tuesday, June 16, 2020 11:47 AM