Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF application that follows a fairly standard MVVM pattern. The UI contains a couple of ComboBox controls that bind to a list of User objects, each of which has a Name property. The ComboBox.DisplayMemberPath is set to this. All good so far.

Sometimes, it makes no sense to have any names displayed in the ComboBox drop-downs. I do this by setting the List<User> in the logic layer to null. This happens at the same time as lots of other things get cleared, and so I call INotifyPropertyChanged with an empty string for the property name to update all bound UI controls. This seems to work with the exception of the ComboBox controls. These keep their last-displayed name.

Is there a way to get the ComboBox in the UI to realise the list it is bound to has been set to null or should I be taking a different approach?

What I have tried:

Setting the SelectedIndex to -1 in the UI fixes this but I have to put an event or similar into the logic to trigger the UI to do this.
Posted
Updated 8-Dec-22 0:16am

1 solution

Hard to tell what is happening unless you provide sample code.

Usually, if you are using data binding in WPF, you have implemented INotifyPropertyChanged and INotifyCollectionChanged for collections/lists. ObservableCollection implements both:
C#
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged

If you are following the data binding requirements, then you may have a binding issue. The latest versions of Visual Studio have a XAML Binding Failures window:
Menu > Debug > Windows > XAML Binding Failures

A third possibility is that you are using public fields and not public properties. You can only data bind to properties, not fields.

If you are still having issues, please update your question and keep it simple and focused.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900