Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax conditions for converters #3433

Closed
FoggyFinder opened this issue Jan 12, 2020 · 11 comments
Closed

Relax conditions for converters #3433

FoggyFinder opened this issue Jan 12, 2020 · 11 comments

Comments

@FoggyFinder
Copy link
Contributor

MCVE

    public class M2Conv : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int v) return v * 2;
            return value;
        }

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

And usage:

public int IntValue { get; } = 42;
<TextBlock Background="Yellow" Text="{Binding IntValue, Converter={StaticResource m2}}}" />

In Avalonia this will not work due to ImplicitConversions restrictions in TryConvertImplicit.

In WPF the same converter works just fine.

Additional thoughts: if I have int property that bound to Text and it works I would expect that if my converter returns int it will work as well.

@grokys
Copy link
Member

grokys commented Jan 13, 2020

Hmm, unsure about this one. Avalonia is asking you to convert object value to targetType and you're instead converting the value to a different type.

I need to look into how/why WPF handles this, but my initial thought is that your converter is broken.

@FoggyFinder
Copy link
Contributor Author

Any decision? :-)

@grokys
Copy link
Member

grokys commented Feb 17, 2020

Sorry, I've not had chance to look into it. Any idea where in the WPF codebase it handles IValueConverter returning a type other than the type it was asked for?

@FoggyFinder
Copy link
Contributor Author

no, sadly

@grokys
Copy link
Member

grokys commented Feb 18, 2020

Just to check: what is targetType when your converter is called?

@FoggyFinder
Copy link
Contributor Author

String

@grokys
Copy link
Member

grokys commented Feb 18, 2020

Yeah, so I really think this is a bug in your code. I know that WPF handles it fine, but there's nothing to my knowledge that says that WPF should accept this, I expect it's a case of it working by accident.

@FoggyFinder
Copy link
Contributor Author

But can it at least gave a warning or something? Because currently it just does nothing so TextBlock is empty.

@FoggyFinder
Copy link
Contributor Author

Anyway warning is a different Q. Closing the issue since this is not a bug.

@grokys
Copy link
Member

grokys commented Feb 18, 2020

Hmm yeah it should be outputting an error to the log. If that's not happening then that is a bug I think.

@FoggyFinder
Copy link
Contributor Author

There is a error/warning message on master. So I will wait until next release is out and then re-check again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants