-1

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?

Muhammad Sulaiman
  • 2,399
  • 4
  • 14
  • 28
  • 1
    To Show a sorted view of a collection, create a `CollectionViewSource`, set its `Source` property to the collection, add the appropriate `SortDescription` to its list of them, and bind your `ItemsControl.ItemsSource` to its view instead of to the source collection. Then if you want another view with a different sorting, do it all again. – Joe Oct 07 '22 at 01:46
  • https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/?view=netdesktop-6.0#binding-to-collections – Clemens Oct 07 '22 at 06:39
  • @Joe The title was about filtering, the question body is about changing the order of the items (not sorting), OP says: "Enum has the options A, B, C, D. At one point I would like to show as the first option D instead of A", so OP wants to change the order from "A, B, C, D" to "D, A, B, C".. It's not about ascending/descending sorting – Muhammad Sulaiman Oct 07 '22 at 06:54
  • 1
    @MuhammadSulaiman, What you have described is called sorting. And sorting (along with filtering) is precisely what collection view source is for. But as Clemens comment indicates, it requires you to read the documentation – Joe Oct 07 '22 at 12:31
  • @joe, I've said:"It's not about ascending/descending sorting".. The docs do not mention custom sorting (changing ABCD to DBCA), neither your comment.. Anyway, this can be done by defining a custom sorter following the steps in [this example](https://stackoverflow.com/questions/24147585/collectionviewsource-with-custom-sort). – Muhammad Sulaiman Oct 07 '22 at 13:18
  • 1
    Yes you did. And I apologize. That was a snarky comment by me – Joe Oct 07 '22 at 13:38

2 Answers2

0

Define a converter that takes MyEnum, change its order based on the value of converter's parameter and return 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 .}"/>
Muhammad Sulaiman
  • 2,399
  • 4
  • 14
  • 28
-1

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.