默认值不能为'Unset'

本文关键字:Unset 不能 默认值 | 更新日期: 2023-09-27 18:01:39

我有一个附加属性,当我将它附加到一个xaml元素时,我得到一个名为的异常"默认值不能为'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));
我的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>

请告诉我哪里做错了

默认值不能为'Unset'

如果我没看错的话,这里写着:

特别不允许设置UnsetValue的DefaultValue。

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

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