IsEditable problems when using ItemTemplate

8 posts, 1 answers
  1. Rob
    Rob avatar
    6 posts
    Member since:
    Sep 2008

    Posted 17 Sep 2008 Link to this post

    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

  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 18 Sep 2008 Link to this post

    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.
  3. DevCraft banner
  4. Rob
    Rob avatar
    6 posts
    Member since:
    Sep 2008

    Posted 18 Sep 2008 Link to this post

    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
  5. Answer
    Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 18 Sep 2008 Link to this post

    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.
  6. Rob
    Rob avatar
    6 posts
    Member since:
    Sep 2008

    Posted 22 Sep 2008 Link to this post

    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.
  7. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 22 Sep 2008 Link to this post

    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.
  8. Sternico
    Sternico avatar
    25 posts
    Member since:
    Sep 2009

    Posted 10 Oct 2012 Link to this post

    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

  9. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 15 Oct 2012 Link to this post

    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.

Back to Top
DevCraft banner