locked
SOS: XAML binding Error: Converter failed to convert value of type '...' to to type '...'

    Question

  • Hi, I'm working on a small Windows store App project using C# + XAML, and encountered a really weird problem with data binding in XAML. The error message is:

    Error: Converter failed to convert value of type 'Neptune.MyMatch, SkyNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' to type 'ProgressableBase'; BindingExpression: Path='' DataItem='Neptune.MyMatch, SkyNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'MyControls.ProgressableViewControl' (Name='root'); target property is 'Progressable' (type 'ProgressableBase').

    Error: Converter failed to convert value of type 'Neptune.MyMatch, SkyNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' to type 'IMatchWrapper'; BindingExpression: Path='' DataItem='Neptune.MyMatch, SkyNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'MyControls.MatchScoreControl' (Name='root'); target property is 'Match' (type 'IMatchWrapper').

    the XAML code snippet involved is:

    <local:MatchScoreControl Match="{Binding Converter={StaticResource TestConverter}}" />

    here:

    1) The type of the 'Match' property of 'MatchScoreControl' is 'IMatchWrapper', the type of the DataContext is 'MyMatch';

    2) 'MyMatch' is derived from 'ProgressableBase' and implemented 'IMatchWrapper';

    3) below is the definition of 'MatchScoreControl' and its dependency property 'Match':

    public sealed partial class MatchScoreControl : UserControl { public IMatchWrapper Match { get { return (IMatchWrapper)GetValue(MatchProperty); } set { SetValue(MatchProperty, value); } } public static readonly DependencyProperty MatchProperty = DependencyProperty.Register("Match", typeof(IMatchWrapper), typeof(MatchScoreControl), new PropertyMetadata(null, OnMatchPropertyChanged)); private static void OnMatchPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // this line will never get called Debug.WriteLine(e.NewValue.GetType().FullName); }

    //more code... }

    4) TestConverter is a dummy value converter(IValueConverter) which does nothing just to make sure the binding object and target type is as expected:

        public sealed class TestConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                if (targetType != typeof(IMatchWrapper))
                {
                    throw new NotImplementedException("Error: targetType is not IMatchWrapper.....");
                }
    
                if (!(value is IMatchWrapper))
                {
                    throw new NotImplementedException("Error: value is not IMatchWrapper...");
                }
    
                return (IMatchWrapper)value;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }

    When I run the app, none of the two exceptions in the TestConverter.Convert() is thrown, which is right and as expected. But the out window keeps throwing the error message as above. This is pretty weird!

    I've tried adding a parameterless constructor to the class 'MyMatch', but it doesn't work.

    Any suggestion would be appreciated!



    Friday, January 16, 2015 9:35 AM

Answers

  • Finally found the solution on SO(stackoverflow.com/questions/10544084/winrt-dependencyproperty-with-ienumerable-not-firing-at-all/). Adding a dummy dependency property of the derived type solved the problem. Explanation:

    "

    Why?

    I suppose the reason is that XAML doesn't know anything about your derived types and only knows about the BaseClass. So creating a dummy dependency property for each type should solve the problem.

    "

    • Marked as answer by silverbird2015 Friday, January 16, 2015 10:52 AM
    Friday, January 16, 2015 10:22 AM

All replies

  • Finally found the solution on SO(stackoverflow.com/questions/10544084/winrt-dependencyproperty-with-ienumerable-not-firing-at-all/). Adding a dummy dependency property of the derived type solved the problem. Explanation:

    "

    Why?

    I suppose the reason is that XAML doesn't know anything about your derived types and only knows about the BaseClass. So creating a dummy dependency property for each type should solve the problem.

    "

    • Marked as answer by silverbird2015 Friday, January 16, 2015 10:52 AM
    Friday, January 16, 2015 10:22 AM
  • Good to know you have the solution and thanks for sharing the experience.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 19, 2015 6:03 AM
    Moderator