Why can't I select a null value in a ComboBox?

后端 未结 10 2090
花落未央
花落未央 2020-12-08 06:11

In WPF, it seems to be impossible to select (with the mouse) a \"null\" value from a ComboBox. Edit To clarify, this is .NET 3.5 SP1.

Here\'s some c

10条回答
  •  难免孤独
    2020-12-08 06:51

    ComboBox needs a DataTemplate to display the item no matter how simple it is. DataTemplate works like this: get a value from instance.[path], e.g.

    bar1.Car.Color
    

    So it cannot get a value from

    null.Car.Color
    

    It will throw a null reference exception. So, the null instance will not be displayed. But the the Color - if it is a reference type - is allowed to be null because there will be no exception in this case.

提交回复
热议问题