【发布时间】:2014-02-26 23:46:36
【问题描述】:
我很难让依赖属性正常工作而不会导致编译错误。
我有一个名为 TitleBar 的用户控件,其中包含此 DependencyProperty:
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(TitleBar), new PropertyMetadata(false));
public string Title
{
get
{
return (string)GetValue(TitleProperty);
}
set
{
SetValue(TitleProperty, value);
}
}
在我看来,在 xaml 中:
<Components:TitleBar x:Name="customTitleBar" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3"/>
导致设计器/编译错误:
Cannot create an instance of "TitleBar". ... The invocation of the constructor on type 'TitleBar' that matches the specified binding constraints threw an exception. System.Exception {System.Windows.Markup.XamlParseException}
用户控件可以在没有 DP 的情况下完美运行。我做错了什么?
奇怪的是,当我实际将 Title="..." 属性放入 xaml 时,它在设计器中工作,直到我尝试编译它。从那时起,它给了我提到的错误。
【问题讨论】:
标签: c# wpf xaml properties