【发布时间】:2011-10-08 22:26:06
【问题描述】:
我正在尝试将 DependencyProperty 添加到 WPF 自定义控件。
在我保留 sn-p propdp 生成的代码之前,一切都很好:
namespace CustomControl
{
public partial class MainControl
{
public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(string), typeof(MainControl), new UIPropertyMetadata(0));
public MainControl()
{
this.InitializeComponent();
}
}
}
但是,当我将类型从“int”更改为“string”时,我得到了一个运行时错误,它告诉 “无法创建在程序集 CustomControl 等中定义的 MainControl 实例......
然后我改回输入“int”,一切都再次正常运行。
有人知道解决这个谜团的线索吗?
【问题讨论】:
-
你确定你已经发布了导致错误的代码吗?该依赖属性为我编译。你错过了什么吗?
标签: c# wpf user-controls