我正在尝试创建一个从ListBoxItem扩展的自定义控件,如下所示:
public class MainNavListBoxItem : ListBoxItem
{
public MainNavListBoxItem()
{
this.DefaultStyleKey = typeof(MainNavListBoxItem);
}
}
我有一个样式,它在ResourceDictionary中定义如下:
<Style TargetType="assets:MainNavListBoxItem">
<Setter Property="Padding" Value="3"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="assets:MainNavListBoxItem">
<Grid Height="50">
<VisualStateManager.VisualStateGroups>
...
现在它编译并运行得很好,但是只要我向MainNavListBoxItem
类添加一个额外的依赖属性,我就会开始收到这样的错误:
无法从文本'Padding'创建'System.Windows.DependencyProperty'。
如果我在样式中重新安排Setter标签,它将始终报告最顶层的标签。
我的依赖属性代码供参考:
public ImageSource ImageDark
{
get { return (ImageSource)GetValue(ImageDarkProperty); }
set { SetValue(ImageDarkProperty, value); }
}
public static readonly DependencyProperty ImageDarkProperty =
DependencyProperty.Register("ImageDark", typeof(ImageSource), typeof(MainNavListBoxItem), new PropertyMetadata(0));
这是怎么回事?!我希望能够在样式中使用此ImageDark依赖项属性!
我已经对此错误进行了大量搜索,但似乎与此问题无关。
答案 0 :(得分:4)
此问题是因为我忘记将我的依赖项属性中的new PropertyMetadata(0)
更改为new PropertyMetadata(null)
。谢谢肯特。