WPF: When is data binding applied?

Michael 26 Reputation points


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)
            Return _BindingData
        End Get
        Set(ByVal value As List(Of String))
            _BindingData = value
        End Set
    End Property

    Sub New()

        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.

I got the advice to post it in MS Q&A (here ist the original post: https://social.msdn.microsoft.com/Forums/vstudio/en-US/9352c02c-2711-4822-a7cd-77c3aaf1c56a/wpf-when-is-data-binding-applied?forum=vbgeneral)

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,316 Reputation points

    Hi, use ObservableCollection instead of List. You can simplified your code like this:

    <Window x:Class="MainWindow"
            Title="MainWindow" Height="450" Width="800">
        <Button Content="new row" Click="Button_Click"/>
        <ComboBox ItemsSource="{Binding BindingData}"/>

    Imports System.Collections.ObjectModel
    Class MainWindow
      Public Property BindingData As New ObservableCollection(Of String)
      Sub New()
        DataContext = Me
        BindingData.Add("Data 1")
        BindingData.Add("Data 2")
      End Sub
      Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        BindingData.Add("new row")
      End Sub
    End Class

1 additional answer

Sort by: Most helpful
  1. Michael 26 Reputation points

    Hi PeterFleischer-3316,

    thank you for your answer, i know the use of ObservableCollection but it does not answer my question.

    I do not understand why the combo box updates his content if a new element is added to the binded List(of String) - BindingData after it was assigned to the combobox ItemsSource. This happens only before the ui/window is shown, maybe it has something to do with that?