Skip Navigation LinksHome / Community & Support / Code Library / Silverlight > General and Integration Projects > A MultiSelect ComboBox

Not answered A MultiSelect ComboBox

Feed from this thread
  • Kakone avatar

    Posted on Sep 23, 2010 (permalink)

    Requirements

    RadControls version 2010.2

     

    .NET version 4

     

    Visual Studio version 2010

     

    programming language C#

     

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    A MultiSelect ComboBox with SelectedItems and SelectedValues properties directly on the ComboBox. You can bind a datasource directly to the combobox, without have IsChecked property on your viewmodel.
    Attached files

    Reply

  • Kakone avatar

    Posted on Sep 30, 2010 (permalink)

    I updated the code, it works better (see attached file).
    Attached files

    Reply

  • Jerry avatar

    Posted on Nov 25, 2010 (permalink)

    Hello Kakone,

    I want to rewrite your MultiSelect ComboBox with WPF code,but I can not trigger the selection event!
    please help me what is wrong with my code!!
    Attached files

    Reply

  • Pinaki Basu avatar

    Posted on Nov 25, 2010 (permalink)

    Hi

    i have also done the same conversion, but it does not work. the  SelectedItemsCollectionChanged event is not getting fired. also, the checkbox's IsChecked property is not hitting the MultiSelectComboBoxService.IsChecked property. please help, need this urgent.

    Maqsood

    Reply

  • wang avatar

    Posted on Nov 30, 2010 (permalink)

    Hi Kakone ,
    thanks for your code, could you add "select All" feature into the control

    for instance :
    1 when i checked the "Select All", all the items will be checked
    2 when i unchecked the "Select All",all the items will be unchecked
    3 currently all the items checked , when i hit an item for uncheck  , "select All" item  will change to uncheck status 

    Reply

  • Posted on Dec 22, 2010 (permalink)

    Hi all,

    I did a multi checked combo.
    It's a bit hardcoded style but it's working... I first took the code from a previous thread  and added some functions.

    There is a select all feature. It's the first item and it has to stay the first.
    I've added a key field in the dataItem and the comboBoxSource.
    If you want to improve my code, you're welcome to do it as I did it very quickly...
    Don't be too harsh on me :) I'm a beginner...

    I'm using silverlight 4 and v.2010.3.1110.1040 of telerik silverlight controls.

    I hope it's going to work for you!

    Reply

  • Posted on Dec 22, 2010 (permalink)

    Hi,

    I changed my previous code because it was not working with several radComboBox.
    I guess it's working now.
    Attached files

    Reply

  • John avatar

    Posted on Mar 17, 2011 (permalink)

    Great control, Biruh.   I was looking for this quite a while. 
    Now I have a question:  How to I obtain the selected values in that MultiSelectComboBox?

    I want to get these values and put into a ObservableCollection<customer> list.  Do you have a simple sample codes for this?
    Thanks

    Reply

  • Posted on Mar 17, 2011 (permalink)

    As I wrote before, I did this control quite quickly and it's not really generic.

    The MyDataItem class is mainly a pair of string. One is representing the value and the other the text displayed in the combo.

    If you just need the values, you can have it by doing something like this :

           //example to get the selected value or text
            private void radCombo1_DropDownClosed(object sender, EventArgs e)
            {
                ComboBoxSource cs = (ComboBoxSource)radCombo1.ItemsSource;

                ObservableCollection<customer> list = new ObservableCollection<customer>();
                //get Value
                foreach (var value in cs.SelectedItemsValue)
                {
                    list.Add(new customer(value));
                }
                
            }





     The SelectedItemsValue property of the ComboBoxSource contains all the selected values. This is a list of <String>... 

    After, I don't know if you are trying to "bind" some instances of customer to the MultiSelect Combo, if it's the case, I guess you should :
    - add a customer(or an object to stay "generic") property to the class MyDataItem
    - add a list of customer(or an object to stay "generic")  property to the class ComboBoxSource
    - add some code to add the customer when it is selected (See when the value is added to the SelectedItemsValue)

    I guess I was clear enough...

    Reply

  • John avatar

    Posted on Mar 18, 2011 (permalink)

    That is all I need. 
    Thanks again for your help. 

    Reply

  • John avatar

    Posted on Mar 18, 2011 (permalink)

    Edited.

    Thanks
    John

    Reply

  • Raphael avatar

    Posted on Mar 18, 2011 (permalink)

    Hi Kakone,

    Thank you for this multiselect combo. I would like to use it in my project but i have a unhandled exception :

    La propriété pouvant être attachée 'SelectionBoxLoaded' est introuvable dans le type 'MultiSelectComboBoxService'. [Line: 5 Position: 153]

    at MS.Internal.XcpImports.CreateFromXaml(String xamlString, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers, Boolean expandTemplatesDuringParse) at System.Windows.Markup.XamlReader.Load(String xaml) at Limagrain.SaisieQuestionnaire.Light.MultiSelectComboBox.MultiSelectComboBox..ctor() in C:\[Acelys-EDR]\Limagrain_SaisieQuestionnaireIndicateurs\Sources\V1.0\Limagrain.SaisieQuestionnaire.Light\MultiSelectComboBox\MultiSelectComboBox.cs:line 43


    Have you got an idea for an issue?

    bye,

    Ralph

    Reply

  • John avatar

    Posted on Mar 23, 2011 (permalink)

    Hi Biruh,
    I am getting very close and have one more question in your sample codes:

    I know when you assign the selectedItem by hardcoded.   How do I mark the checked items dynamically?

    Here is my situation:
    Declare the control in .xaml like yours:
    <input:RadComboBox 
           x:Name="radCombo1"   SelectedIndex="0"  Height="20" Width="150" DropDownClosed="radCombo1_DropDownClosed">
        <input:RadComboBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Path=Text}" IsChecked="{Binding Path=IsSelected,Mode=TwoWay}" Height="16" HorizontalAlignment="Left" Margin="2" VerticalAlignment="Top" />
            </DataTemplate>
        </input:RadComboBox.ItemTemplate>
        <input:RadComboBox.SelectionBoxTemplate >
            <DataTemplate>
                <TextBlock   Text="{Binding ElementName=radCombo1, Path=ItemsSource.SelectedItemsText}" />
            </DataTemplate>
        </input:RadComboBox.SelectionBoxTemplate>
    </input:RadComboBox>

    At .xaml.cs codebehind file, I bind that control with the collection from DB as:
    void client_GetCustomerListCompleted(object sender, CService.GetCustomerListCompletedEventArgs e)
    {
        if (e.Result != null)
        {
            ClearMultiSelection(); //clear all checked one first
            radCombo1.ItemsSource = e.Result;
        }
    }

    So far, it lists all the customers in the combobox. 
    I also have a list of customers that need to be selected in that combobox.  How would I do it?

    Thanks much, again, for your help.
    John.

    Reply

  • Posted on Mar 25, 2011 (permalink)

    Sorry I didn't really get what you wanted to do...
    You want to bind directly a list of customers to the radcombo?

    Reply

  • Roby avatar

    Posted on Apr 27, 2011 (permalink)

    Great work on this control it works like a champ.

    Reply

  • Nandita avatar

    Posted on Jun 29, 2011 (permalink)

    Hi,

    I am not able to select the check/uncheck check boxes with the spacebar. How do i achieve this?

    Reply

  • jfkrueger Master avatar

    Posted on Nov 13, 2011 (permalink)

    I'm trying to port this to VB.net and am just having trouble converting the following bit of code:

    protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if (e.NewItems != null)
                {
                    foreach (MyDataItem item in e.NewItems)
                    {
                        item.PropertyChanged += new PropertyChangedEventHandler(OnItemPropertyChanged);
                    }
                }
                if (e.OldItems != null)
                {
                    foreach (MyDataItem item in e.OldItems)
                    {
                        item.PropertyChanged -= new PropertyChangedEventHandler(OnItemPropertyChanged);
                    }
                }
                base.OnCollectionChanged(e);
                this.UpdateSelectedText();
            }


    The telerik code converter comes up with this but it is not working:

    Protected Overrides Sub OnCollectionChanged(ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
        If e.NewItems IsNot Nothing Then
            For Each item As MyDataItem In e.NewItems
                item.PropertyChanged += New PropertyChangedEventHandler(OnItemPropertyChanged)
            Next
        End If
        If e.OldItems IsNot Nothing Then
            For Each item As MyDataItem In e.OldItems
                item.PropertyChanged -= New PropertyChangedEventHandler(OnItemPropertyChanged)
            Next
        End If
        MyBase.OnCollectionChanged(e)
        Me.UpdateSelectedText()
    End Sub

    Is it this simple:

    Protected Overrides Sub OnCollectionChanged(ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
        If e.NewItems IsNot Nothing Then
            For Each item As MyDataItem In e.NewItems
                AddHandler item.PropertyChanged, AddressOf OnItemPropertyChanged
            Next
        End If
        If e.OldItems IsNot Nothing Then
            For Each item As MyDataItem In e.OldItems
                AddHandler item.PropertyChanged, AddressOf OnItemPropertyChanged
            Next
        End If
        MyBase.OnCollectionChanged(e)
        Me.UpdateSelectedText()
    End Sub
     

    It gives me the "item.PropertyChanged is an event and cannot be called directly..." line. I know how to raise events in vb.net using RaiseEvent but it doesn't look like that is what I need to be doing here. Any help?

    THanks!

    Reply

  • jfkrueger Master avatar

    Posted on Nov 13, 2011 (permalink)

    Okay I believe I figured out my first question (yes it was really that simple) but now I am stuck on replacing the hard-coded data with data retrieved from a WCF service. Almost everything works great...BUT the only thing that doesn't work for some reason is the SelectedItemsText seems to stop working. The drop-down populates correctly and the checkboxes work great (including the "All") checkbox, but when I switch to loading the items from a service the text is no longer appearing on the combo box. Here is my code:

    Private Sub initCombo()
      
        'create the source of the checkedCombo
        'Dim cs As New ComboBoxSource()
      
        ''Hard coded select All Item
        'cs.Add(New MyDataItem("All", "All", True))
      
        ''Add other Items you want
        'cs.Add(New MyDataItem("001", "001", True))
        'cs.Add(New MyDataItem("002", "002", True))
        'cs.Add(New MyDataItem("003", "003", True))
        'cs.Add(New MyDataItem("004", "004", True))
      
        ''DataBinding
        'radCombo1.ItemsSource = cs
      
        Dim lSelectedGroup As String = CType(RadComboBoxGroups.SelectedValue, GroupData).GroupNumber
        Dim lGroupService As New GroupService.GroupDashboardServiceClient
      
        AddHandler lGroupService.GetDivisionsForGroupForDashboardCompleted, AddressOf GetDivisionsForGroupForDashboardCallback
      
        lGroupService.GetDivisionsForGroupForDashboardAsync(lSelectedGroup, True)
      
    End Sub
      
    Private Sub GetDivisionsForGroupForDashboardCallback(ByVal sender As Object, ByVal e As GroupService.GetDivisionsForGroupForDashboardCompletedEventArgs)
      
        If DesignerProperties.IsInDesignTool Then
            Return
        End If
      
        Dim lRecords As List(Of Dictionary(Of String, String)) = e.Result
        'create the source of the checkedCombo
        Dim cs As New ComboBoxSource
      
        For Each lRecord As Dictionary(Of String, String) In lRecords
            cs.Add(New MyDataItem(lRecord("Division").ToString.Trim, lRecord("Division").ToString.Trim, True))
        Next
      
        'DataBinding
        radCombo1.ItemsSource = cs          
      
    End Sub

    I don't understand why it would matter that I am pulling the data from a service when it comes to populating the SelectedItemsText. The really strange thing is that it begins working correctly once I click off to the right of one of the combobox items (to the right of the text where it doesn't check/uncheck the checkbox but just closes the drop-down). Once that happens, everything works correctly but I need it to work on initial load and without the user having to do this (like it does in your example).

    Anyone have any ideas on what is causing this?

    Thanks!!

    Reply

  • Roby avatar

    Posted on Nov 14, 2011 (permalink)

    I have used this control in a couple of place and found to make the already selected items appear in the textblock for the combobox you will need to handle the loaded event handler and loop through the selected items on the object you are viewing and add them to the selecteditems property on the combo box. Hope this helps.

    Reply

  • jfkrueger Master avatar

    Posted on Dec 4, 2011 (permalink)

    Hey Roby, thanks for the post!

    I am not quite getting what you are saying though...

    If I am in the ComboBox loaded event handler, what is "the object i am viewing"?

    Thanks!

    Reply

  • Michael avatar

    Posted on Mar 27, 2012 (permalink)

    Hi, I am using Kakones original multiselectcombo. I am trying to bind the result of the combo to a string as it appears in the combo i.e. I have an AddressType field that I would like to contain one or more values from the dropdown list, e.g. "Invoice, Registered Office, Delivery". As an extra complication I have this in a RadGridView. Can someone please give me a pointer on how I can acheive this, here is an example of the XAML I would like to end up with....
    <telerik:GridViewDataColumn UniqueName="AddressType" >
        <telerik:GridViewDataColumn.CellEditTemplate>
            <DataTemplate>
                <local:MultiSelectComboBox x:Name="comboBox" ItemsSource="{Binding Source={StaticResource Menus}, Path=GetAddressType}"
                DisplayBindingPath="DisplayName" SelectedValuePath="KeyValue"
                 SelectedValueText="{Binding AddressType}"/>
                 
            </DataTemplate>
        </telerik:GridViewDataColumn.CellEditTemplate>
    </telerik:GridViewDataColumn>

    Reply

  • Roby avatar

    Posted on Mar 27, 2012 (permalink)

    The textblock on the control holds the display values in a comma deliminated string. To add already selected values to the list all you need to do is handle the loaded event and add the items you wish selected to the SelectedItems collection. They should appear in the display when the combobox is closed.
    To clear up the question on my previous post: the object I am referring to is say a Form of some kind. On this form one of the fields is Locations to visit lets say. The combobox's itemssource is bound to a collection of places. On the loaded event just loop through the Locations collection on the form object and add each location object to the combobox's selecteditems collection.

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Code Library / Silverlight > General and Integration Projects > A MultiSelect ComboBox