This is a migrated thread and some comments may be shown as answers.

IsEditable problems when using ItemTemplate

7 Answers 335 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 17 Sep 2008, 12:12 PM
Hello,

I am using a RadComboBox with an ItemTemplate, bound to an ItemsSource (a GenericList of CLR objects) to make a sort of rich autocomplete combobox.

But when the user selects an item, the RadComboBox text field is filled with a ToString() result of the Objet that is selected.
The string displayed in the combobox after an item is selected is similar to: "Mynamespace.MyObject".

I tried setting the RadComboBox.Text property but that doesn't work. How can I bind the editable textfield to a property of the selected object? Or at least get something else than a ToString() result?

Edit:
Another weird thing is that when a user selects, for example, the 2nd item in the list, the SelectionChanged event is triggered 4 times with the following selected items:
  1. The selected item itself
  2. null
  3. The selected item itself
  4. The first item in the list

Edit 2:
If an item is selected and the user starts typing in the editable filed of the RadComboBox and you set the ItemsSource again, the editable field gets cleared.

Edit 3:
When using DisplayMemberPath, it works when using the defualt itemtemplate, but when I use a custom ItemTemplate, it doesn't seem to work.


To make things clear, this is what I am trying to achieve; I am trying to make a editable ComboBox that uses autocomplete via a WCF service. When a user fills in a part of the name, a WCF call is made and the ItemsSource is updated. When a user selects an Item, the name of the Item is filled in the ComboBox. When a user decides to change the field, the WCF call is made again, setting a new ItemsSource to the ComboBox. This may not overwrite the new searchstring the user has typed, like it does now. 

Kind Regards,

Rob

7 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 18 Sep 2008, 11:47 AM
Hi Rob,

Regarding your questions:
when the user selects an item, the RadComboBox text field is filled with a ToString() result of the Objet that is selected
This is by design. To avoid it, use DisplayMemberPath, or a custom ItemTemplate. Note that DisplayMemberPath will not work in conjunction with ItemTemplate. For more information and simple examples for DisplayMemberPath and custom ItemTemplate, please, visit the following help article:
http://www.telerik.com/help/silverlight/binding-to-collections.html
and the online demo:
http://demos.telerik.com/silverlight/#Examples/ComboBox/DataBinding

If an item is selected and the user starts typing in the editable filed of the RadComboBox and you set the ItemsSource again, the editable field gets cleared
I would suggest instead of setting ItemsSource, to modify the collection - RadComboBox will synchronize its items.

I created a simple application (attached), that demonstrates how to modify the ItemsSource. Note that you should use ObservableCollection<T>, or a collection that implements INotifyCollectionChanged if you want RadComboBox to automatically synchronize its items.

Regards,
Valeri Hristov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 18 Sep 2008, 02:25 PM
Hi Valeri,

Regarding your answers:
Note that DisplayMemberPath will not work in conjunction with ItemTemplate.

Is there any solution that will be implemented in the future? I really like the combobox, but I would like it to be editable and with a custom item template, with a proper "selected item" text inside the editable textfield.

I created a simple application (attached), that demonstrates how to modify the ItemsSource. Note that you should use ObservableCollection<T>, or a collection that implements INotifyCollectionChanged if you want RadComboBox to automatically synchronize its items.

Thank you for the solution!

Regards,

Rob
0
Accepted
Valeri Hristov
Telerik team
answered on 18 Sep 2008, 02:49 PM
Hello Rob,

I finally understood what you need! :) Here is how you can display a single member of the selected data item in the combo box's text box, and use it when autocompleting:

<UserControl x:Class="SilverlightTestApp.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
  xmlns:i="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <StackPanel x:Name="LayoutRoot" Background="White">
  <i:RadComboBox ItemsSource="{Binding}" IsEditable="True" telerik:TextSearch.TextPath="Value">

   <i:RadComboBox.ItemTemplate>
    <DataTemplate>
     <StackPanel>
      <TextBlock Text="{Binding Key}" />
      <TextBlock Text="{Binding Value}" />
     </StackPanel>
    </DataTemplate>
   </i:RadComboBox.ItemTemplate>
  </i:RadComboBox>
 </StackPanel>
</UserControl>


The page code-behind is:

public partial class Page : UserControl
{
 private ObservableCollection<DataItem> Items { get; set; }

 public Page()
 {
  InitializeComponent();
  Items = new ObservableCollection<DataItem>();
  Items.Add(new DataItem() { Key = "Key1", Value = "Value1" });
  Items.Add(new DataItem() { Key = "Key2", Value = "Value2" });
  Items.Add(new DataItem() { Key = "Key3", Value = "Value3" });
  this.DataContext = Items;
 }
}

public class DataItem
{
 public string Value { get; set; }
 public string Key { get; set; }
}

I hope this helps.

Greetings,
Valeri Hristov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 22 Sep 2008, 07:10 AM
Thanks alot for the support! This is what I was looking for :).

Now there is only one detail that bugs me, and I have tried working around it but nothing worked out so far; when a user hits the spacebar when the dropdown is opened, there is no space inserted. It seems that hitting space does something with the selecteditem?

What I am looking for is that users can type searchstrings with spaces in them, while the dropdown of the combobox is open.
0
Valeri Hristov
Telerik team
answered on 22 Sep 2008, 07:52 AM
Hi Rob,

This is a known problem in RadComboBox, it will be fixed in the upcoming update of RadControls for Silverlight, scheduled for the next week.

All the best,
Valeri Hristov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Sternico
Top achievements
Rank 1
answered on 10 Oct 2012, 01:09 PM
Hallo Valeri,

i had the same Problem like Rob and your example from Sep 18, 2008 works fine. There is only a little Point.
I want to have a ComboBox where the items have an image and a text. And autocompleting over the text. All this works fine, but i need the image in the selected item, too. Now there is only the text (the property which is set over 'telerik:TextSearch.TextPath').

Thanks Sico

0
Vladi
Telerik team
answered on 15 Oct 2012, 10:52 AM
Hi Sico,

Unfortunately it is not possible to achieve the described scenario in RadComboBox.

If you have any other questions feel free to contact us again.

Greetings,
Vladi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
ComboBox
Asked by
Rob
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Rob
Top achievements
Rank 1
Sternico
Top achievements
Rank 1
Vladi
Telerik team
Share this question
or