A MultiSelect ComboBox

25 posts, 0 answers
  1. Kakone
    Kakone avatar
    65 posts
    Member since:
    Jan 2010

    Posted 23 Sep 2010 Link to this post

    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.
  2. Kakone
    Kakone avatar
    65 posts
    Member since:
    Jan 2010

    Posted 30 Sep 2010 Link to this post

    I updated the code, it works better (see attached file).
  3. Jerry
    Jerry avatar
    7 posts
    Member since:
    Oct 2010

    Posted 25 Nov 2010 Link to this post

    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!!
  4. Pinaki Basu
    Pinaki Basu avatar
    15 posts
    Member since:
    Aug 2009

    Posted 25 Nov 2010 Link to this post

    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
  5. wang
    wang avatar
    1 posts
    Member since:
    Jul 2010

    Posted 30 Nov 2010 Link to this post

    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 
  6. Biruh MEKONNEN
    Biruh MEKONNEN avatar
    23 posts
    Member since:
    Apr 2010

    Posted 22 Dec 2010 Link to this post

    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!
  7. Biruh MEKONNEN
    Biruh MEKONNEN avatar
    23 posts
    Member since:
    Apr 2010

    Posted 22 Dec 2010 Link to this post

    Hi,

    I changed my previous code because it was not working with several radComboBox.
    I guess it's working now.
  8. John
    John avatar
    34 posts
    Member since:
    Oct 2010

    Posted 17 Mar 2011 Link to this post

    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

  9. Biruh MEKONNEN
    Biruh MEKONNEN avatar
    23 posts
    Member since:
    Apr 2010

    Posted 17 Mar 2011 Link to this post

    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...

  10. John
    John avatar
    34 posts
    Member since:
    Oct 2010

    Posted 18 Mar 2011 Link to this post

    That is all I need. 
    Thanks again for your help. 
  11. John
    John avatar
    34 posts
    Member since:
    Oct 2010

    Posted 18 Mar 2011 Link to this post

    Edited.

    Thanks
    John
  12. Raphael
    Raphael avatar
    15 posts
    Member since:
    Jan 2011

    Posted 18 Mar 2011 Link to this post

    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
  13. John
    John avatar
    34 posts
    Member since:
    Oct 2010

    Posted 23 Mar 2011 Link to this post

    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.
  14. Biruh MEKONNEN
    Biruh MEKONNEN avatar
    23 posts
    Member since:
    Apr 2010

    Posted 25 Mar 2011 Link to this post

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

  15. Roby
    Roby avatar
    3 posts
    Member since:
    Jul 2010

    Posted 27 Apr 2011 Link to this post

    Great work on this control it works like a champ.
  16. Nandita
    Nandita avatar
    9 posts
    Member since:
    Mar 2011

    Posted 29 Jun 2011 Link to this post

    Hi,

    I am not able to select the check/uncheck check boxes with the spacebar. How do i achieve this?
  17. jfkrueger
    jfkrueger avatar
    269 posts
    Member since:
    Jul 2012

    Posted 13 Nov 2011 Link to this post

    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!
  18. jfkrueger
    jfkrueger avatar
    269 posts
    Member since:
    Jul 2012

    Posted 13 Nov 2011 Link to this post

    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!!
  19. Roby
    Roby avatar
    3 posts
    Member since:
    Jul 2010

    Posted 14 Nov 2011 Link to this post

    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.
  20. jfkrueger
    jfkrueger avatar
    269 posts
    Member since:
    Jul 2012

    Posted 04 Dec 2011 Link to this post

    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!
  21. Michael
    Michael avatar
    21 posts
    Member since:
    Apr 2011

    Posted 27 Mar 2012 Link to this post

    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>
  22. Roby
    Roby avatar
    3 posts
    Member since:
    Jul 2010

    Posted 27 Mar 2012 Link to this post

    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.
  23. Pushpendra
    Pushpendra avatar
    32 posts
    Member since:
    Nov 2012

    Posted 03 Dec 2013 Link to this post

    Hi All,

    I am using MultiSelectCombo and it is working fine. Now when user check-uncheck the checkboxes for particular items I want to perform some action like show-hide other controls. Just wanted to know how can I achieve this functionality, since I am not able to get CheckBox itself or its IsChecked property or Checked event.

    Thanks,

    Pushpendra

  24. Tamil
    Tamil avatar
    1 posts
    Member since:
    Nov 2012

    Posted 22 May 2014 in reply to Biruh MEKONNEN Link to this post

    Great work. 

    Thanks,
    Thamizh
  25. Sameer
    Sameer avatar
    1 posts
    Member since:
    Jan 2015

    Posted 12 Jan Link to this post

    Could anybody please post complete .xaml,c# code in this post..

    I cannot download the .zip files as it is restrcited in proxy.

     I want complete code to be pasted in post 

     

    Thanks in advance for the help.

Back to Top