Why Does Binding to a Struct Not Work? Why Does Binding to a Struct Not Work? wpf wpf

Why Does Binding to a Struct Not Work?


Your binding gets a copy of struct since structs are passed by value to methods. If the binding updates something; a copy in memory somewhere is being modified and hence the original object of yours is not updated.


Because the struct is passed by value to the control, therefore when you make changes in the UI, WPF writes the changes back to a different instance of People.

Change it to a class and it'll work.

Unless you fully understand the purpose of the struct I suggest not using it.


The ListView is an ItemsControl and works either in direct mode where you populate it's Items by declaring severl ListViewItem objects in XAML or in ItemsSource mode where you set a Binding on the ItemsSource property

See this Dr. WPF article for a good explanation.

Either way, the ListView.Items is an ItemCollection which is a CollectionView, i.e. Items is not the actual collection you supply to the ItemsSource property but a normalized copy of the collection you supplied which allows the framework to for example access an underlying IEnumerable by index even though IEnumerable doesn't provide an indexer itself.

Since the ListView uses a copy, when it is using a collection of Class instances it can make copies of the references, both references point to the same object in memory so the effects of changing values in one of the references is visible through the other reference, but when it is using a collection of structs which are value types it has to make copies of the values, rather than having two references which point to the same object, you then have two distinct value type objects.