默认值不能为“未设置”

时间:2015-07-09 04:45:16

标签: c# wpf xaml attached-properties

当我将它附加到xaml元素时,我有一个附加属性我得到一个名为的异常“默认值不能'未设置'”

我的附属属性逻辑

    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));

我的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>

请让我知道我在哪里做错了。

2 个答案:

答案 0 :(得分:2)

如果我正确阅读this,请说明:

  

特别禁止设置UnsetValue的DefaultValue。

所以我建议设置一个默认值,如null或更适合您的用例。

答案 1 :(得分:1)

DependencyProperty.UnsetValue不是依赖项属性的有效默认值。除非您想指定null以外的默认值,否则根本不需要注册属性元数据。

您还必须将FooterContentTemplateProperty的声明更改为使用RegisterAttached而不是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));