Passing viewmodel properties as parameters in ValueConverters in .Net MAUI

Parvez ali 6 Reputation points
2022-06-20T09:21:51.86+00:00

I am trying to create a custom converter in .Net MAUI app but I am not able to pass ViewModel properties as parameter.

This is my Xaml code. -

<Button.BackgroundColor>  
                            <Binding Path="Category">  
                                <Binding.Converter>  
                                    <converters:TypedTodoCategoryConverter x:TypeArguments="Color"  
                                                                           AllCategory="{StaticResource SkyBlue}"  
                                                                           TodayCategory="{StaticResource Purple}"  
                                                                           UpcomingCategory="{StaticResource Orange}"  
                                                                           CompletedCategory="{StaticResource Green}"  
                                                                           OverdueCategory="{StaticResource Red}"  
                                                                           Unselected="Transparent"/>  
                                </Binding.Converter>  
                            </Binding>  
                        </Button.BackgroundColor>  

This is my converter code.

public class TypedTodoCategoryConverter<T> : ValueConverter<TodoCategory, T>  
    {  
        public T AllCategory { get; set; }  
        public T UpcomingCategory { get; set; }  
        public T CompletedCategory { get; set; }  
        public T OverdueCategory { get; set; }  
        public T TodayCategory { get; set; }  
        public T Unselected { get; set; }  
  
        protected override T Convert(TodoCategory value, Type targetType, object parameter, CultureInfo culture)  
        {  
            if(parameter != null)  
            {  
                bool isSelected = (bool)parameter;  
                switch (value)  
                {  
                    case TodoCategory.All:  
                        return isSelected ? AllCategory : Unselected;  
                    case TodoCategory.Today:  
                        return isSelected ? TodayCategory : Unselected;  
                    case TodoCategory.Upcoming:  
                        return isSelected ? UpcomingCategory : Unselected;  
                    case TodoCategory.Completed:  
                        return isSelected ? CompletedCategory : Unselected;  
                    case TodoCategory.Overdue:  
                        return isSelected ? OverdueCategory : Unselected;  
                    default:  
                        return Unselected;  
                }  
            }  
            else  
            {  
                switch (value)  
                {  
                    case TodoCategory.All:  
                        return AllCategory;  
                    case TodoCategory.Today:  
                        return TodayCategory;  
                    case TodoCategory.Upcoming:  
                        return UpcomingCategory;  
                    case TodoCategory.Completed:  
                        return CompletedCategory;  
                    case TodoCategory.Overdue:  
                        return OverdueCategory;  
                    default:  
                        return default;  
                }  
            }  
        }  
    }  
  
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,177 questions
{count} votes