首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >默认值不能是“Unset”

默认值不能是“Unset”
EN

Stack Overflow用户
提问于 2015-07-09 04:45:15
回答 2查看 544关注 0票数 0

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

我的附加属性逻辑

代码语言:javascript
运行
复制
    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:

代码语言:javascript
运行
复制
     <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>

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-09 06:18:41

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

您还必须更改FooterContentTemplateProperty的声明,以使用RegisterAttached而不是Register

代码语言:javascript
运行
复制
public static readonly DependencyProperty FooterContentTemplateProperty =
    DependencyProperty.RegisterAttached(
        "FooterContentTemplate", typeof(DataTemplate), typeof(RadWindowFooterProperties));

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

Stack Overflow用户

发布于 2015-07-09 04:53:07

好吧,如果我正确地阅读了,它会说:

特别不允许设置DefaultValue of UnsetValue。

因此,我建议设置一个默认值,如null或任何更适合您的应用程序的值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31308304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档