locked
XAML Error : Value Type Nullable is not allowed on property in XAML

    Question

  • I have defined a DependencyProperty of type Nullable<Boolean> in my custom control.

    Example:

      public Nullable<Boolean> IsEnabled
            {
                get
                {
                       return (Nullable<Boolean>)GetValue(IsEnabledProperty);
                }
                set
                {
                    SetValue(IsEnabledProperty, value);
                }
            }

         public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register
                ("IsEnabled",
                typeof(Nullable<Boolean>),
                typeof(CustomControl),
                new PropertyMetadata(null));

    Now when I am creating the custom control though xaml and setting this property, it is throwing exception "Value Type Nullable is not allowed on property IsEnabled in XAML".

    Note: When I am creating the control at code behind and setting the property, it's working well.

    I even tried changing the property type from Nullable<Boolean> to Object in DP.Register but still it is throwing this exception. Can anybody please tell me what could be the problem? It works properly in Silverlight and WPF.

    Tuesday, April 17, 2012 10:42 AM

Answers

  • If I change the type to Object in the Property instead of in the Register, your code runs:

    public Object IsEnabled
    {
        get
        {
            return (Nullable<Boolean>)GetValue(IsEnabledProperty);
        }
        set
        {
            SetValue(IsEnabledProperty, value);
        }
    }
    
    public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register
            ("IsEnabled",
            typeof(Nullable<Boolean>),
            typeof(CustomControl),
            new PropertyMetadata(null));

    • Marked as answer by Min ZhuMember Monday, April 30, 2012 9:39 AM
    Tuesday, April 17, 2012 5:37 PM
  • Jordan,

    The XAML compiler error you are running into will be fixed in next public release of Visual Studio but you won't be able to set the Nullable properties in Custom Control in XAML since it's not a supported scenario. I would recommend either setting these properties in code or not using Nullable properties if you intend to set them through XAML.

    If you have any questions, please send me an email at navits at microsoft dot com.

    -Navit Saxena

    • Proposed as answer by navits - MSFT Tuesday, April 17, 2012 11:45 PM
    • Marked as answer by Min ZhuMember Monday, April 30, 2012 9:39 AM
    Tuesday, April 17, 2012 11:45 PM

All replies

  • If I change the type to Object in the Property instead of in the Register, your code runs:

    public Object IsEnabled
    {
        get
        {
            return (Nullable<Boolean>)GetValue(IsEnabledProperty);
        }
        set
        {
            SetValue(IsEnabledProperty, value);
        }
    }
    
    public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register
            ("IsEnabled",
            typeof(Nullable<Boolean>),
            typeof(CustomControl),
            new PropertyMetadata(null));

    • Marked as answer by Min ZhuMember Monday, April 30, 2012 9:39 AM
    Tuesday, April 17, 2012 5:37 PM
  • Jordan,

    The XAML compiler error you are running into will be fixed in next public release of Visual Studio but you won't be able to set the Nullable properties in Custom Control in XAML since it's not a supported scenario. I would recommend either setting these properties in code or not using Nullable properties if you intend to set them through XAML.

    If you have any questions, please send me an email at navits at microsoft dot com.

    -Navit Saxena

    • Proposed as answer by navits - MSFT Tuesday, April 17, 2012 11:45 PM
    • Marked as answer by Min ZhuMember Monday, April 30, 2012 9:39 AM
    Tuesday, April 17, 2012 11:45 PM
  • Hi,

    This issue has been fixed in Visual Studio 2012 RC release. Please try installing Windows8 Release Preview with Visual Studio 2012 RC.

    -Navit Saxena

    Thursday, May 31, 2012 8:42 PM
  • Hi Navit, 

    I got this ("Value Type Nullable is not allowed on property IsEnabled in XAML".) exception in the Visual Studio 2012 RC release. Please see my code

    public static readonly DependencyProperty EnableAntialiasingProperty = DependencyProperty.Register("EnableAntialiasing",
                typeof(Nullable<Boolean>), typeof(SeriesView), new PropertyMetadata(null, InvalidateRendering));

    public Nullable<Boolean> EnableAntialiasing {
                get { return (Nullable<Boolean>)GetValue(EnableAntialiasingProperty); }
                set { SetValue(EnableAntialiasingProperty, value); }
            }

    What is wrong here?

    Thursday, August 16, 2012 9:47 AM