views:

191

answers:

1

Hi,

I would like to use autocompletebox with a list of values, but also add new values to the list if a user enters one that isn't present.

I currently have a string property in my view model called 'Comment'. Currently this is bound to a textbox in the view - user types a comment and the view model is updated. Simple.

To save time, my customer would like it to autocomplete from previous values, so the way I thought to do this is something like:

ViewModel

public string Comment;
public ObservableCollection<string> CommentsList { ... }

(populate the CommentsList when the viewmodel is created)

View

<AutoCompleteComboBox ItemsSource="{Binding CommentsList}" SelectedItem="{Binding Comment, Mode=TwoWay/>

So when the user selects a value, it saves the value in the Comment property. This works fine if the user selects an item in the list, but if the user types in a new value, the comment property is not updated (it is null because the selected item was not in the list).

Is this possible with autocompletebox?

Thanks in advance, Will

+1  A: 

Found the solution...

I needed to use the Text property, not SelectedItem.

Text Contains the current text that the user has entered or selected.

SelectedItem Is null if no item is currently selected. If the user navigates through the drop down, selecting items, or clicks on an item, this will be updated to that item.

See:

http://www.jeff.wilcox.name/2008/10/introducing-autocompletebox/

WillH
+1 looks good. sorry the control isn't a little easier to use the control with mvvm.
Jeff Wilcox