views:

394

answers:

2

I have a WPF listbox control that is declaratively bound to a textbox. The listbox's ItemsSource is an ObservableCollection that is built from an XML file. I can easily prevent duplicate entries in the listbox when a new item is added because I can check for it in the "Add" button's Click event handler.

However, when an existing item's value is changed in the textbox (which obviously shows the listbox's selected item) to one that already exists in the list I want to prevent this, but I don't know how.

I'd appreciate help with this!

+1  A: 

Listen to the CollectionChanged event and check when the collection has been modified if there are any duplicates and remove them.

Also, you can take a look at this question and its' answer for an observable collection that also notifies you when its' items' properties change.

Edit:

If you don't want to use the collection I mentioned above, you can make sure your collection's items implement INotifyPropertyChanged and every time you add an item to the collection, listen to its PropertyChanged event. In the handler, you check if the property that changes is the one that is displayed in the ListBox and check if any other element has the same value of this property. If you find such an element, you either change the value of your property to its old value, or remove the element entirely, it depends on the logic of your application.

luvieere
I think that won't work - since the textbox is bound to an item, you're not changing the collection at all. You'd have to remove/readd the item for the CollectionChanged to be triggered.
Julien Lebosquain
Yes, but you can use an Observable collection implementation that does notify the elements' modification. I've edited my answer to reflect this.
luvieere
I've looked at Soren's solution and it looks promising. However my lack of C# experience precludes me from extending his ObservableCollectionEx class to work for me. For example, his ReactToChange event handler does not take any properties so how would I know which item triggered the event and what property was changed?
Gerhard
I've edited my answer with a new idea. Hope that it helps.
luvieere
How would I listen to the PropertyChanged event? Isn't this what Soren's ObservableCollectionEx does?
Gerhard
Yes, but as you've mentioned, as it is, it doesn't tell you which property was changed.
luvieere
+3  A: 

You can create your own validation rule by deriving from ValidationRule and apply it to your text box's binding. In the Validate method you can check for duplicates and return a ValidationResult of false to prevent the binding source from being updated.

Julien Lebosquain
This is the way to do it! Have a look here: http://msdn.microsoft.com/en-us/library/ms753962.aspx
Dabblernl
Correct me if I'm wrong, but do Validation Rules not concern themselves with the values of the specific list item and not the list itself? In other words, how would I check in the validation rule for duplicates in the list containing the item when the item doesn't know it is part of a list?
Gerhard