默认值不能是 'Unset'
Default value cannot be 'Unset'
我有一个附加的 属性 当我将其附加到 xaml 元素时我收到一个名为 "Default value cannot be 'Unset'"[=13 的异常=]
我的附件属性逻辑
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>
请告诉我哪里做错了。
好吧,如果我读 this 正确,它会指出:
Setting a DefaultValue of UnsetValue is specifically disallowed.
所以我建议设置一个默认值,例如 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));
我有一个附加的 属性 当我将其附加到 xaml 元素时我收到一个名为 "Default value cannot be 'Unset'"[=13 的异常=]
我的附件属性逻辑
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>
请告诉我哪里做错了。
好吧,如果我读 this 正确,它会指出:
Setting a DefaultValue of UnsetValue is specifically disallowed.
所以我建议设置一个默认值,例如 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));