Answered Binding doesn't work for TypeInfo Class.

  • Thursday, April 26, 2012 12:28 PM
     
      Has Code

    I create 2 classes (TestClass1 and TestClass2 ) and add them to Page as resources. Property of TestClass2 binds to property of TestClass2 but this doesn't work.

     public class TestClass1
        {
            public TypeInfo Property
            {
                get
                { return typeof(string).GetTypeInfo(); }
            }
        }
    
        public class TestClass2:DependencyObject
        {
            public TypeInfo Property
            {
                get { return (TypeInfo)GetValue(PropertyProperty); }
                set { SetValue(PropertyProperty, value); }
            }
            
            public static readonly DependencyProperty PropertyProperty =
                DependencyProperty.Register("Property", typeof(TypeInfo), typeof(TestClass2), new PropertyMetadata(null));
    
            public string PropertyString
            {
                get
                {
                    if (this.Property != null)
                    {
                        return this.Property.FullName;
                    }
                    else
                    {
                        return "Property didn't set";
                    }               
                }
            }        
        }

    XAML:

    <Page.Resources>
            <local:TestClass1 x:Key="Res1"/>
            <local:TestClass2 x:Key="Res2" Property="{Binding Source={StaticResource Res1}, Path=Property}"/>
        </Page.Resources>
        <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
            <TextBlock Name="tb" Text="{Binding Source={StaticResource Res2}, Path=PropertyString}"/>
        </Grid>

    When I run this code, textblock shows text "Property didn't set". If I change type of properties from TypeInfo to custom class, everything works fine.

    Is it correct behavior?

All Replies

  • Friday, April 27, 2012 7:37 PM
    Moderator
     
     
    I'm looking into this.  One interesting thing I found when I turned on binding logging:

    Error: Converter failed to convert value of type 'System.RuntimeType, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' to type 'TypeInfo'; BindingExpression: Path='Property' DataItem='test_TypeInfo_Binding.TestClass1'; target element is 'test_TypeInfo_Binding.TestClass2' (Name='null'); target property is 'Property' (type 'TypeInfo').

    I'll continue to investigate and update you when I know more.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator

  • Tuesday, May 01, 2012 1:45 PM
    Moderator
     
     Answered
    This is a known problem.  The suggestion is to try using Object as the property type instead of TypeInfo.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator