RadComboBox Multiple Selection problem with enum source

2 posts, 1 answers
  1. Cesar
    Cesar avatar
    5 posts
    Member since:
    Dec 2010

    Posted 08 Aug Link to this post

    Hello,

    I have a RadcomboBox with Multiple Selection allowed. The ItemSource of this combo is a enum.

     public enum WorkStatus   {

            [Description("WORKING")]
            Working,
            [Description("NOT WORKING")]
            Not_Working,
            [Description("WORKING WITH ERRORS")]
            Working_with_errors,
            [Description("STOPPED")]
            Stopped
        }

    And this is my code:

    <Window x:Class="MultiSelectComboboxEnums.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
            xmlns:local="clr-namespace:MultiSelectComboboxEnums"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="MainWindow"
            Height="350"
            Width="525">

        <Window.Resources>
            <ObjectDataProvider x:Key="statesWork"
                                MethodName="GetValues"
                                ObjectType="{x:Type sys:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:WorkStatus" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>

            <local:EnumToFriendlyNameConverter x:Key="enumToFriendlyNameConverter" />
        </Window.Resources>

        <Grid>
            <telerik:RadComboBox Width="350"
                                 Height="26"
                                 Margin="20"
                                 HorizontalAlignment="Center"
                                 VerticalAlignment="Center"
                                 telerik:StyleManager.Theme="Windows8"
                                 ItemsSource="{Binding Source={StaticResource statesWork}}"
                                 ClearSelectionButtonVisibility="Visible"
                                 IsSynchronizedWithCurrentItem="False"
                                 AllowMultipleSelection="True"
                                 MultipleSelectionSeparator="|"
                                 MultipleSelectionSeparatorStringFormat="{} {0} ">
                <telerik:RadComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label Height="Auto"
                               Margin="0"
                               VerticalAlignment="Center"
                               Content="{Binding Path=., Mode=OneWay, Converter={StaticResource enumToFriendlyNameConverter}}" />
                    </DataTemplate>
                </telerik:RadComboBox.ItemTemplate>
            </telerik:RadComboBox>
        </Grid>
    </Window>

     

    In dropdown item I can see the correct name using a converter but how can I do to see the same in the selected items box?

    I attached two images showing this error.

    Thanks in advance.

  2. Answer
    Kalin
    Admin
    Kalin avatar
    1279 posts

    Posted 11 Aug Link to this post

    Hi Cesar,

    This is so because the converter you are using in the DataTemplate is not applied in selection box (their string representation is used). As a solution you could apply a custom MultipleSelectionBoxTemplate and use the same converter there as well. Check the following snippet:

    <telerik:RadComboBox.MultipleSelectionBoxTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=telerik:RadComboBox}, Path=SelectedItems}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Label Height="Auto"
                Margin="0"
                VerticalAlignment="Center"
                Content="{Binding .. Converter}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </telerik:RadComboBox.MultipleSelectionBoxTemplate>

    Please note that when using custom MultipleSelectionBoxTemplate the separator won't be applied - you would need to add it to the template. For more details please check the following article:
    http://docs.telerik.com/devtools/wpf/controls/radcombobox/features/multiple-selection

    Hope this helps.

    Regards,
    Kalin
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top