1

In my Control I have a ListBox. Each ListBoxItem is displayed (via ItemTemplate) as a Combobox whose ItemsSource is an ObservableCollection in the ViewModel of my Control.

Next to that ComboBox is a Button to delete an element from the list (the Button's Click Command is bound to a Delete-Command in the ViewModel of my control). This works fine as desired.

Now I want to be able to add new elements to the ListBox. I'd know how to do this by clicking a Button either somewhere outside the ListBox or in the ListBox's ItemTemplate, but instead I would like to add some kind of additional "empty" item as the last item of my ListBox.

This "empty" item should look like any other item, with the exception that the ComboBox has no selection. As soon as the user makes a selection, a new "empty" item has to be displayed. I hope you know what I mean...acutally it's a bit like the "Tags" editor when you post a new question here ;)

Any ideas??? (without breaking MVVM rules)...

1
  • If i understand right it seems that you need to add a property to your model - isEmpty. This property should be bound to the ComboBox.Visible property Commented Jul 23, 2015 at 11:35

1 Answer 1

1

Instead of a ListBox, use a DataGrid with CanUserAddRows="True". It will add that "new line" row, and behave exactly as you want.

Sign up to request clarification or add additional context in comments.

2 Comments

this works fine (so I marked this answer as "accepted"). Anyway I do not really like using a DataGrid here, DataGrid seem like a too mighty control for a little "problem" like this. Other solutions keeping the original ListBox (or maybe ListView) are still welcome.
I've tried to come up with a "simple" way of doing this, by using CompositeCollection and event handling, but the outcome is pretty dirty and not too MVVM-ish. The correct way would be inheriting from ListBox and handling all the logic (placeholder item, selection and focus changes, etc.) inside the control itself, but it is no trivial task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.