Problem clearing BindableCollection when datanbound against Combobox

Sep 1, 2011 at 8:15 PM

I use Caliburn micro, I have a problem where the framework will through an exception (Can not find view for System.String) if i clear the list that the combobox is databound to. Its not bound to a String but a ViewModel of type MappingsSettingModel



<ComboBox x:Name="Mappings" MinWidth="200"></ComboBox>



public BindableCollection<MappingSettingModel> Mappings { get; set; }
public MappingSettingModel SelectedMapping
    get { return selectedMapping; }
        selectedMapping = value;
        NotifyOfPropertyChange(() => SelectedMapping);

It works if i change the COmbobox to a ListView or a ItemsControl,why do i get errors when using a combobox?

It works if i remove the SelectedMapping property, But i need that so that I can set which itemn shouldbe selected..

Sep 1, 2011 at 9:13 PM

ComboBox doesn't appear to have a convention out of the box.  At least its not in the list of conventions in the documentation.  Therefore it would through an error about not know what you want.

Sep 1, 2011 at 10:13 PM
Edited Sep 1, 2011 at 10:14 PM

It must be because Seleced<NameofCollionInSingular> convention does work, it would work if its not in the convention. Everything works, except when I clear the list it throws the above error, and it does not work to set the SelectedMapping property to null, it throws the same exception.

Sep 1, 2011 at 10:23 PM
Edited Sep 1, 2011 at 10:25 PM

Strange i've narrowed down the problem further, its if I set the Selected-property from code that the problem happens...

edit: No sorry, you dont even need to set it by code, the problem happens if you have a notifyproperty change on the Selected-property

Anyone knows whats going on?

Sep 1, 2011 at 11:14 PM
Edited Sep 1, 2011 at 11:14 PM

just for giggles what platform are you doing your dev on?

Sep 1, 2011 at 11:40 PM

WPF, Visual Studio 2010, .NET 4.0

Sep 2, 2011 at 12:37 AM
Edited Sep 2, 2011 at 12:45 AM

so I played with this on an app i could do the similar operation with, I clear the contents of the collection, (subsequently sets the selecteditem to null) and I can refill, and can select the item, but without error... Are you using the SelectedMapping for anything after you clear the list, without checking for null?

 The one difference is that I am not binding to a ViewModel, I am binding to a collection of aircraft... something in your viewmodel might be reacting to null in a very bad way.

Sep 2, 2011 at 3:09 PM
Edited Sep 2, 2011 at 3:10 PM

The only way to reproduce the error is if your selected property raises a NotifyPropertyChanged event, so if you use a POCO which does not inherit from PropertyChangeBase you wont get the error.

I use the ItemsControl until I can solve this, its a bit sad though because it takes more space if all items are showing

Heres what I'm trying todo, each command (Take off, Land etc) can have multiple input mappings, in the pic below i use a ItemControl, but if you have many mappings it takes alot of space, with a combobox only one mapping will show  and you can press the arrow to see them all

Sep 2, 2011 at 3:30 PM

so when you clear the list are you setting SelectedMappings = null?  Only way to really narrow this down would be to try ObservableCollection<> instead of BindableCollection<> to truly see if it is an issue with BC.

Sep 3, 2011 at 12:51 AM

Well if i set it to null it crashes with the same error, Clear collection or set SelectedMapping to null gives the same error.

I could try a ObservableCollection, the neat thing with Caliburns collection is that it works all the magic with Dispathers and stuff, since its my directx thread that updates teh ViewModel in this case it will crash if I use a ObservableCollection out of the box.. And you dont want dispather logic in your viewmodels, I guess thats why Caliburn gave us the BC