1
我试图创造出从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依赖项属性!
我已经做了很多关于这个错误的搜索,但没有任何东西似乎与这个问题有关。
不确定这是没有测试相关的,但您指定默认样式键的方式不正常。您应该在静态构造函数中重写元数据。这可能会让WPF在你的实例构造函数中做到这一点。例如,请参阅[这里](http://wpftutorial.net/HowToCreateACustomControl.html)。 – 2013-02-24 18:58:04
你不能覆盖silverlight中的元数据,你能吗? – caesay 2013-02-24 18:58:41
对不起,以为这是WPF。 – 2013-02-24 19:00:31