0

How can I set a combobox to null in C# using WPF?

I have implemented two languages (German and English). Always when I select one, all comboboxes are cleared. That works fine. The problem is, if a item of a combobox is selected, that the application crashes.

German: Der Wert darf nicht NULL sein. Parametername: source

English: The value cannot be NULL. Parameter name: source

Does anyone know how to fix the problem?

sjantke
  • 605
  • 4
  • 9
  • 35
  • How are you clearing the combobox? – Guilherme Feb 07 '14 at 18:17
  • Like this: comboBox_1.Items.Clear(); – sjantke Feb 07 '14 at 18:25
  • Post your current code. It is very likely that you're doing a `.FirstOrDefault()` or other LINQ operation on a `null` collection. This has nothing to do with WPF and is an exception caused by a bug in your code. Also, please learn MVVM before ever writing a single line of code in WPF. – Federico Berasategui Feb 07 '14 at 18:32
  • @HighCore "Also, please learn MVVM before ever writing a single line of code in WPF" Yeah, MVVM is good, but it sounds a bit overrated for me... Dunno, maybe MVVM is not the best solution in 100% of time – Guilherme Feb 07 '14 at 18:43
  • @Guilherme Yeah, I'm pretty sure hacking your way thru UI virtualization using `VisualTreeHelper` is really much better than using proper DataBinding... Please.... – Federico Berasategui Feb 07 '14 at 18:44
  • @HighCore I agree with you that MVVM is good in almost any situation, but what I mean is that is not 100% of the situations. For example, in a Window that you have sure that will be ever with the same Model, and you are using a lot of events, like the mouse right click in some elements, or other events... Is a lot easier to write this without MVVM, IMO... Into some simple code behind. – Guilherme Feb 07 '14 at 18:51
  • @GUilherme I don't understand your last comment. And no, nothing is "easier" if you disregard DataBinding and use archaic winforms-like techniques in WPF. BTW, MVVM has nothing to do with any mouse events or the like. That's a UI concern which MVVM does not care about. MVVM is about DataBinding and decoupling the business logic and the UI. – Federico Berasategui Feb 07 '14 at 18:53
  • @HighCore This is the point. What is easier for you? or this – Guilherme Feb 07 '14 at 18:58
  • How you use MouseDoubleClick in a MVVM app? – Guilherme Feb 07 '14 at 18:58
  • @Guilherme **again**, MVVM **does not care** about mouse stuff, that belongs to the UI. If you want to execute a method from the VM from code behind, that's fine. MVVM is about **separating business logic and UI**. You have a serious misunderstanding of what MVVM is. – Federico Berasategui Feb 07 '14 at 19:01
  • This is a great example: http://stackoverflow.com/questions/501886/wpf-mvvm-newbie-how-should-the-viewmodel-close-the-form?rq=1 – Guilherme Feb 07 '14 at 19:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47045/discussion-between-highcore-and-guilherme) – Federico Berasategui Feb 07 '14 at 19:02
  • Maybe you are right, this is what I learn about MVVM. Could you recommend me please a good book or a complete tutorial? – Guilherme Feb 07 '14 at 19:02
  • @Guilherme BTW, yes, an `InputBinding` is really much better than a code behind approach, because: 1 - it takes care of disabling the UI element when `CanExecute()` is false and 2 - you may define many `InputBinding`s to the same `Command` without having to add a bunch of duplicated code behind events. – Federico Berasategui Feb 07 '14 at 19:40

2 Answers2

0

Before

comboBox_1.Items.Clear();

Do

comboBox_1.SelectedIndex = -1;
Guilherme
  • 5,143
  • 5
  • 39
  • 60
0

You shouldn't call Clear() on the ComboBox.Items you should either clear or set to NULL the underlyining collection in your ViewModel to which your ComboBox is bound to and which is the data source of your ComboBox Items...

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • Thanks, but the collection cannot be set to NULL. I get the same error message while working with the combobox during runtime, just like selecting an item of the combobox. It stops at once and I get that error message. Annother idea? – sjantke Feb 08 '14 at 12:55