Silverlight AutoCompleteBox SelectedValue(?) - silverlight

I need to implement an editable combobox where users can select existing values from the data/tables. It needs to be editable because users can also add new rows to the table by entering new values in the editable combobox, so I put an AutoCompleteBox control into my page but I can't find any sample on how to implement such feature. It should display something like Employee Name in the editable dropdown while having the SelectedValue property to contain the Employee ID.
Any help will be very much appreciated.
Cheers!

You will need to bind your autocompletebox's ItemsSource to your "lookup" collection.
You can use ValueMemberBinding to resolve the textual input to look for, ie if you have a list of people, bind this to Model.Name like this {Binding Name} to find people by name.
As far as the drop down items, you could use templating to display the items the way you like.
Heres a good tut on the subject, you want to style the ItemTemplate. following from the example you would make a datatemplate in xaml within the ItemsTemplate element add a Textblock and bind its Text property to Name like {Binding Name}.
Here a nice example where a autocompletebox is styled like a combobox. You could extend that to look for "enter" on TextChanged and check to see if the item is contained in the ItemsSource. If not it could push the new value to the server (if you are into MVVM, you could raise a command on you ViewModel that would delegate the addition to the server and update the Items).

Here's another example that extends the AutoCompleteBox to be used as a type-ahead ComboBox. It can handle foreign keys / lookup ids using DPs and can be used in MVVM scenarios.
AutoComplete ComboBox for Silverlight

Related

How can I apply AutoGenerateColumn to apply DataGridTemplateColumn.HeaderTemplate in a DataGrid?

for an overview, an MVVM project in built in WPF.
Basically in my xaml, I have a datagrid bound to a dataview. When my service populates the dataview I get a dynamic table with an arbitrary number of columns.
For each column in the datagrid, I have created a headertemplate which contains a combobox which is bound to an Observable<Dictionary<string,BusinessEntity>> object as its item source in the xaml. Figuring out the combobox is another issue but I am trying to just populate data grid first, then worry about binding the combobox correctly.
anyways the only solution I have somewhat though of was to turn on autogeneratecolumn and then replacing all the headers with a combobox in the codebehind, but then I have issues trying to bind the combobox in the codebehind correctly and it doesn't feel MVVM if I have to create all those comboboxes there.
You can handle the AutoGeneratingColumn event to customize the auto generated column's header template.

DataGridTemplateColumn Binding Using a TextBox in WPF with MVVM

I have a DataGrid that is bound to a List (ListOne). Inside the grid I'd like to display an extra TextBox that contains fields found in another Class that is not the same as the Class used in ListOne.
I've done this before with a ComboBox using the DataGridTemplateColumn, the problem is I'm not sure how the Binding would work when using a TextBox?
ListOne contains code and description which is bound and displays correctly. My SQL Stored Procedure returns extra values using a Join, which I'd like to display as editable fields ListOne.
Any ideas?
You should probably try to bind the DataGrid directly to the joined data as i imagine that doing joins directly in the cells may be difficult and round-about.

How to implement an editable listview in MVVM model?

I have an mvvm application...I need to have an editable listview.
I am binding my observable collection to listview.
How could I track changes of the values in listview ?...I mean if user edit an item...I need to have changed values in my observable collection.
If I use datagrid in WPFToolKit, is it easy ?
In a word, yes.
Take a look at data templates in WPF. They allow you to define how you want each item in your list (or any control) to appear and behave. So each item in your listview can one or more editable controls that are bound to each item in your collection (in this case an ObservableCollection). As you change data in the listview, the bound objects will in your collection will be updated in real time.
This is also possible with a datagrid.
Have a look at this link
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit
It is recommended you to use the Datagrid.It already provides the edit mode functionality. You can use a TemplateColumn to provide editing views.
if you have an editable Collection in your viewmodel, just take a datagrid(editable stuff built in). you can create styles or use templates so that the datagrid look the way you want.
If I use datagrid in WPFToolKit, is it easy ?
yes ;) but if you can, use the .net4 datagrid

WPF, Binding a combobox and textbox to each other AND data source

I have a two column combobox, and a textbox, bound to xml data.
The textbox shows the equivalent of the comboboxes second column of the currently selected item.
I've bound the datacontext of the textbox to the SelectedItem in the combobox, which then updates if you select a row in the combobox. Now, I'd it so that if you type something into the textbox that corresponds to a value in the 2nd column of the combobox, it selects that row.
I realise this is slightly circular.
I've managed it before in winforms, by effectively suspending events when the CombobBox OnSelectedItemChanged fires and updates the textbox or OnTextChange fires and updates selectedItem.
The idea is that the user can either select an option from the combo, or if they know a short code (in this case, country ISO), they can just type it in and immediately see the appropriate country selected in the combobox.
Is it somehow possible to bind the selectedItem in the combobox to the textBox in addition to the underlying data (and indeed does that idea make any sense?), or possibly do some sort of two-way-binding between these elements?
I'm hoping there's a simpler solution than dependencyproperties-- ideally something purely in xaml, but appreciate as I'm new at WPF, I've no idea if this is even possible.
Thanks!
Mike
If you've got a ViewModel you can two-way bind each to the same property of that, and this is probably the best way to do it.

Append Items to Databound ItemsControl in WPF

I've got a Combo Box that is databound to an ObservableCollection of items. I would like to have a default selected item that is (None) that would set the value of the property I've bound to "SelectedValue" to null.
I think there ought to be a way to achieve this with some combination of Style/DataTemplate/TemplateSelector. I'm trying to design this with MVVM in mind, so I'd like something that doesn't use codebehind and is as reusable as possible. I'd also like the benefits of the ObservableCollection (updating the collection causing the control to rebind) to remain intact.
Bonus part B:
I would like to also be able to append an extra visual element to the bottom of an ItemsControl as well. I was thinking it would be easy to change the DataTemplate if I knew how to trigger it on the last item of a collection. Willing to entertain other options here.
The simplest way I've found to do this is to insert a "special" value into the underlying collection, and display the "(None)" text when it's selected. Obviously then you need to run your binding through a converter to take this value into account and return null when it's selected. (See this question of mine which was a result of me trying to add an actual null value to a ComboBox's underlying collection.)
Having said that, it might actually be possible to do what you want with the CompositeCollection class. You could make a separate collection (with only one item - your Null item) and bind your ComboBox to both it and your original collection through the CompositeCollection.

Resources