1

I've got a combobox

<ComboBox ItemsSource="{Binding MyItems}" 
          DisplayMemberPath="DisplayedName" 
          SelectedValue="{Binding MySelectedItem}"/>

and strangely, there is a blank, empty item at the last position when I open the combobox. Why is that? The last, empty entry is also selectable. The collection I am binding to does not contain an empty entry. It contains e.g. 4 items, but the combobox has 5.

EDIT:

The property in my ViewModel:

public ObservableCollection<MyItem> MyItems { get; set; }
private MyItem mySelectedItem { get; set; }
public MyItem MySelectedItem {
    get { return mySelectedItem }
    set { 
             mySelectedItem = value;
             RaisePropertyChanged(() => MySelectedItem);
         }
}

I'm setting MyItems to a collection of available items:

MyItems = AvailableItems();
...
public ObservableCollection<MyItem> AvailableItems()
{
    return new ObservableCollection<MyItem>()
    {
        new MyItem() {DisplayedName = "ItemNameIMadeUp1"},
        new MyItem() {DisplayedName = "ItemNameIMadeUp2"}
    };
}

And I am defining a certain item as selected item:

MySelectedItem = MyItems[0];

EDIT2 I've found out that the "empty" item is a NewItemPlaceholder. What I have not found out is how to remove that or even why it is added to the ObservableCollection :o(

peter
  • 2,103
  • 7
  • 25
  • 51
  • 5
    consider showing the collection code – Hossein Narimani Rad May 05 '15 at 17:08
  • 1
    An ObservableCollection has no SelectedItem property, so what do you expect to be provided by `{Binding MyItems.SelectedItem}`? – Clemens May 05 '15 at 17:29
  • 1
    I have had this happen when an ObservableCollection gets bound to a datagrid, and the framework adds the new item placeholder. Then binding the same ObservableCollection to a combobox did also show the empty item at the bottom. – Paul Gibson May 05 '15 at 17:30
  • @Clemens, you are right, of course, I should have copied and pasted the code. I've corrected the typo. – peter May 05 '15 at 17:33
  • Did you try using `selectedIndex=0` instead of selectedValue? I didn't get a placeholder then I did that – matrixanomaly May 05 '15 at 17:38
  • @matrixanomaly, SelectedIndex="0" did not change anything, unfortunatly. – peter May 05 '15 at 18:16
  • Try to create a break point at the 'MySelectedItem' property setter to see the selected object nature! – Benzara Tahar May 05 '15 at 20:04
  • There are a few mistakes in the code you posted: the DisplayMemberPath is DisplayName, but the property is DisplayedName and the MySelectedItem property is private, so the binding has no access to it. – Domysee May 05 '15 at 20:13
  • Dominik, I am sorry, I typed the coding and should have copied it. In my coding, it is as you write. – peter May 06 '15 at 20:09
  • @peter are you sure you dont add an empty item somewhere? As shown in this [answer](http://stackoverflow.com/questions/1188402/combobox-with-empty-item) you have to explicitly define an empty field – Domysee May 06 '15 at 20:18
  • Dominik, I definitly see that there is no empty item in the binding source. When I select the empty item, I'm not running into the breakpoint I have defined in the setter of 'MySelectedItem'. Very strange. – peter May 06 '15 at 20:33
  • When I define a ComboBox like this '', I see that the entries are displayed with their class name and the last entry is displayed with '{NewItemPlaceholder}' ... that seems like a first trace.. – peter May 06 '15 at 20:51
  • [Here is what you are looking for](http://stackoverflow.com/questions/20521082/how-to-remove-newitemplaceholder-at-treeview-wpf). It explains why it is showing up and a solution how to remove it. (Though for a different control type, but that should be easy to convert) – Frank J May 06 '15 at 21:18
  • Thanks Frank J +1, ;o) just figured it out, see my answer. – peter May 06 '15 at 21:19

2 Answers2

2

I finally figured it out, this was driving me mad.. The reason for the "empty" entry is that I use the same binding for a DataGrid. This binding adds the "NewItemPlaceholder". Crazy that this effects the ComboBox, really crazy.. definitly unexpected for me and, in my opinion, bad design.

The solution is to either use two different ObservableCollections for the Combox and the DataGrid or to set the DataGrid's CanUserAddRows-property to "False". Simple as that. Crazy.

Here and here I found some useful help.

peter
  • 2,103
  • 7
  • 25
  • 51
0

Not sure if this is causing the problem but you still should fix it

You should be binding to SelectedItem not SelectedValue.
You don't even have a SelectedValuePath defined.

SelectedItem

SelectedValue

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Come on down vote. What is the problem? – paparazzo May 05 '15 at 17:55
  • I'm not the downvoter ;o) but I have to tell you that changing to SelectedItem did not change anything, unfortunatly. +1 for you, though, thanks for the response. – peter May 05 '15 at 18:17
  • You should still fix that. I think it is using SelectedItem because you don't have a SelectedValuePath defined. If you define SelectedValuePath it will fail - I tested and it did fail. – paparazzo May 05 '15 at 19:04