Home > front end >  How to sort ItemsSource dynamically in C#; WPF
How to sort ItemsSource dynamically in C#; WPF

Time:10-07

I use in XAML an ItemsControl where and in its ItemsSource I make a Binding for an Enum, thus creating a list of RadRadioButton dynamically, and if someday another item is added to this enumerator my code will already create this new button and show it.

<ItemsControl ItemsSource="{Binding MyEnum}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <telerik:RadRadioButton GroupName="GroupMyEnum">
                <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/>
            </telerik:RadRadioButton>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl> 

Today I use a converter that takes the description of the Enum and shows instead of the value of the Enum.

  • But besides that I would like to change the order in which my list of buttons is generated, is this possible?

Example: If my list needs to be generated elsewhere in the interface, it must be generated in a different order than the enumerator was created.

Whether an Enum has the options A, B, C, D. At one point I would like to show as the first option D instead of A.

Was I clear enough?

CodePudding user response:

Define a converter that takes MyEnum, change its order based on the value of converter's parameter and returns the new list with the new order (ConverterParameter is optional, do not set it if you don't want to change the order of the list).

public class MyEnumCollectionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is IEnumerable<MyEnum> input)
        {
            switch (parameter)
            {
                case "Case1":
                    // todo: change the order of input
                    return input;
                case "Case2":
                    // todo: change the order of input
                    return input;
            }
        }

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Define the converter in your app.xaml (or at one of ItemsControl parents) and use it like this..

<ItemsControl ItemsSource="{Binding MyEnum, Converter={StaticResource MyEnumCollectionConverter}, ConverterParameter=Case1}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <telerik:RadRadioButton GroupName="GroupMyEnum">
                <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/>
            </telerik:RadRadioButton>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl> 

Note that you can do the EnumDescriptionConverter work inside MyEnumCollectionConverter, so your TextBlock can be just

<TextBlock Text="{Binding .}"/>

CodePudding user response:

The first thing that pops to mind is sorting the ItemSource itself. I believe you use Linq sort by calling List.Sort() on the source itself and the list of order on the visual layer should be altered aswell. More information about this can be found here.

  • Related