microsoft / XamlBehaviors

This is the official home for UWP XAML Behaviors on GitHub.
MIT License
700 stars 112 forks source link

DataTriggerBehavior doesn't work with enum types #136

Closed P4bl3r closed 5 years ago

P4bl3r commented 6 years ago

DataTriggerBehavior fails on InvalidCastException when Value is set to enum type.

Context here: https://stackoverflow.com/a/49308851/5405256

I have narrowed the problem down to TypeConverterHelper class and I do believe it can be fixed by adding an Convert method overload to TypeConverterHelper:

public static Object Convert(string value, Type destinationType)
{
    var typeInfo = destinationType.GetTypeInfo();

    if (typeInfo.IsEnum)
        return Enum.Parse(destinationType, value);

    return Convert(value, destinationType.FullName);
}

and updating the call in DataTriggerBehaviors Convert method to use the Type instead of Type.FullName

if (leftOperand != null && rightOperand != null)
{
    rightOperand = TypeConverterHelper.Convert(rightOperand.ToString(), leftOperand.
}
pdecostervgls commented 5 years ago

Duplicate of #84 although with more context. Maybe someone should create a PR for this? Or are there any design reasons for not implementing it this way?

skendrot commented 5 years ago

Thanks @P4bl3r for the investigation! I'm going to close this issue as a duplicatre, but would you like to submit a PR to #84?