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

How to sort the RadComboBoxItems Alphabetically?

4 Answers 817 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Vinnarasi
Top achievements
Rank 1
Vinnarasi asked on 01 Oct 2013, 01:13 PM
Hi,
   Items listed in the RadComboBox need to be sorted Alphabetically.
   How to Alphabetically sort the RadComboBoxItems?

With Thanks,
Vinnarasi

4 Answers, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 03 Oct 2013, 02:22 PM
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 >>
0
Matthias
Top achievements
Rank 1
answered on 11 Mar 2015, 03:05 PM
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

0
Kalin
Telerik team
answered on 12 Mar 2015, 08:52 AM
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.

 
0
Alan
Top achievements
Rank 1
answered on 28 Oct 2015, 07:51 PM
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}" />
Tags
ComboBox
Asked by
Vinnarasi
Top achievements
Rank 1
Answers by
Kalin
Telerik team
Matthias
Top achievements
Rank 1
Alan
Top achievements
Rank 1
Share this question
or