How to sort the RadComboBoxItems Alphabetically?

5 posts, 0 answers
  1. Vinnarasi
    Vinnarasi avatar
    14 posts
    Member since:
    Aug 2013

    Posted 01 Oct 2013 Link to this post

    Hi,
       Items listed in the RadComboBox need to be sorted Alphabetically.
       How to Alphabetically sort the RadComboBoxItems?

    With Thanks,
    Vinnarasi

  2. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 03 Oct 2013 Link to this post

    Hi Vinnarasi,

    In order to sort the items in the ComboBox you would only need to sort the collection bound to the ComboBox ItemsSource in the code behind. For example if this is your ViewModel:

    public ObservableCollection<string> Items { get; set; }
     
    public ViewModel()
    {
        this.Items = new ObservableCollection<string>
        {
            "rrr",
            "bbb",
            "aaa"
        };
    }

    You have to sort it in the code behind as follows:

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
     
        var viewModel = this.DataContext as ViewModel;
        viewModel.Items = new ObservableCollection<string>(viewModel.Items.OrderBy(x => x));
    }

    Hope this will help you.

    Regards,
    Kalin
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Matthias
    Matthias avatar
    1 posts
    Member since:
    Jan 2012

    Posted 11 Mar 2015 in reply to Kalin Link to this post

    Hi Kalin,
    I have a similar problem. The difference is that I use a converter for my display value, like this:

                                        <telerik:RadComboBox.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Id, Converter={StaticResource LabelTypeToNameConverter}}"/>
                                            </DataTemplate>
                                        </telerik:RadComboBox.ItemTemplate>

    I want to sort by the displayed value, i.e. by the result of the converter. How can I achieve this?

    Kind regards

    Matthias

  5. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 12 Mar 2015 Link to this post

    Hi Matthias,

    You can bind the SelectedItem to a property in the ViewModel in order to reorder the collection once the selection is changed. However if this doesn't help - please provide some more information and sample of the exact scenario.

    Hope this will work for you.

    Regards,
    Kalin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Alan
    Alan avatar
    1 posts
    Member since:
    Apr 2012

    Posted 28 Oct 2015 Link to this post

    One also can use CollectionViewSource.  However, this can be slow if there is a large quantity of items.

    Here is how I did it for a collection of strings to be sorted alphabetically by string.

    In VB.NET data context:

        ReadOnly Property MyTags As ObservableCollection(Of String)

    In XAML:

    <UserControl x:Class="MyTagUC"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
         xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
         xmlns:regions="clr-namespace:Microsoft.Practices.Prism.Regions;assembly=Microsoft.Practices.Prism"             
         xmlns:Resources="clr-namespace:My.UI.Utilities;assembly=My.UI"
         mc:Ignorable="d"
    >
        <UserControl.Resources>
            <CollectionViewSource x:Key="MyTagCollectionViewSource" Source="{Binding Path=MyTags}" >
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription />
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </UserControl.Resources>
        <telerik:RadComboBox x:Name="cmbMyTag" ItemsSource="{Binding Source={StaticResource MyTagCollectionViewSource}}" SelectedItem="{Binding SelectedMyTag}" />
    </UserControl>

    Here is how I did it for a collection of objects to be sorted by alphabetically by a string property on an interface implemented by the class of the objects.

    In VB.NET data context:

        Public Interface IMyEntity
            ReadOnly Property MyName As String
        End Interface

        ReadOnly Property MyEntities As ObservableCollection(Of IMyEntity)

    In XAML:

        <UserControl.Resources>
            <CollectionViewSource x:Key="MyEntityCollectionViewSource" Source="{Binding Path=MyEntities}" >
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription PropertyName="MyName" />
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </UserControl.Resources>
        <telerik:RadComboBox x:Name="cmbMyEntity" DisplayMemberPath="MyName" ItemsSource="{Binding Source={StaticResource MyEntityCollectionViewSource}}" SelectedItem="{Binding SelectedMyEntity}" />
Back to Top
UI for WPF is Visual Studio 2017 Ready