user2408987
user2408987

Reputation: 101

Default value cannot be 'Unset'

I have a attached property when I attach this to a xaml element I am getting an exception called "Default value cannot be 'Unset'"

My Attached property logic

    public static DataTemplate GetFooterContentTemplate(DependencyObject obj)
    {
        return (DataTemplate)obj.GetValue(FooterContentTemplateProperty);
    }

    public static void SetFooterContentTemplate(DependencyObject obj, DataTemplate value)
    {
        obj.SetValue(FooterContentTemplateProperty, value);
    }
    // Using a DependencyProperty as the backing store for GetFooterContentTemplate.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FooterContentTemplateProperty =
        DependencyProperty.Register("FooterContentTemplate", typeof(DataTemplate), typeof(RadWindowFooterProperties), new PropertyMetadata(DependencyProperty.UnsetValue));


    public static Object GetFooterContent(DependencyObject obj)
    {
        return (Object)obj.GetValue(FooterContentProperty);
    }

    public static void SetFooterContent(DependencyObject obj, Object value)
    {
        obj.SetValue(FooterContentProperty, value);
    }

    // Using a DependencyProperty as the backing store for RadWindowFooterContent.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FooterContentProperty =
        DependencyProperty.RegisterAttached("FooterContent", typeof(Object), typeof(RadWindowFooterProperties), new PropertyMetadata(DependencyProperty.UnsetValue));

My XAML:

     <Grid x:Name="FooterRoot"  Grid.Row="2" MinHeight="42">
                        <Border Background="{StaticResource ShellFooterBrush}"/>
                        <Border Background="{DynamicResource ShellTileBrush}"/>
                        <ContentPresenter Content="{Binding Path=(Local:RadWindowFooterProperties.FooterContent),RelativeSource={RelativeSource Mode=TemplatedParent}}" ContentTemplate="{Binding Path=(Local:RadWindowFooterProperties.FooterContentTemplate),RelativeSource={RelativeSource Mode=TemplatedParent}}" RecognizesAccessKey="True"/>

                    </Grid>

Please let me know where i am doing mistake.

Upvotes: 0

Views: 660

Answers (2)

Clemens
Clemens

Reputation: 128087

DependencyProperty.UnsetValue is not a valid default value for a dependency property. Unless you want to specify a default value other than null, you don't need to register property metadata at all.

You would also have to change the declaration of FooterContentTemplateProperty to use RegisterAttached instead of Register:

public static readonly DependencyProperty FooterContentTemplateProperty =
    DependencyProperty.RegisterAttached(
        "FooterContentTemplate", typeof(DataTemplate), typeof(RadWindowFooterProperties));

public static readonly DependencyProperty FooterContentProperty =
    DependencyProperty.RegisterAttached(
        "FooterContent", typeof(Object), typeof(RadWindowFooterProperties));

Upvotes: 1

ckruczek
ckruczek

Reputation: 2410

Well if I read this correctly it states:

Setting a DefaultValue of UnsetValue is specifically disallowed.

So I would recommend to set a default value like null or anything more appropriate for your usecase.

Upvotes: 2

Related Questions