默认样式的XamlParseException用于自定义控件属性的Setter

时间:2012-11-10 10:54:28

标签: c# xaml custom-controls winrt-xaml xamlparseexception

请原谅这个迟钝的标题,不知道如何最好地表达我的问题......

我在项目Foo中编写了一个自定义控件“CustomControl”。它有一个依赖属性“Title”,它是一个字符串:

public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register(
        "Title",
         typeof(string),
         typeof(CustomControl),
         PropertyMetadata.Create("Default Title")
    );
public string Title
{
    get { return (string)GetValue(TitleProperty); }
    set { SetValue(TitleProperty, value); }
}

在我的Generic.xaml文件中,我试图用Setter节点设置“Title”的值:

<Style TargetType="local:CustomControl">

    <!-- Setter for Template property -->

    <Setter Property="Title" Value="Any String" />
</Style>

应用程序在尝试渲染控件时会立即窒息,说它无法在我的控件中找到“标题”属性:

  

Foo.exe中出现“Windows.UI.Xaml.Markup.XamlParseException”类型的异常,但未在用户代码中处理

     

WinRT信息:在'Foo.Controls.CustomControl'类型中找不到属性'Title'。 [行:X位置:Y]

     

其他信息:未指定的错误

我在这里做错了吗?这是不允许的? 当我打破调试器时,我可以看到对象上的Title属性,所以我非常困惑。

1 个答案:

答案 0 :(得分:2)

我觉得很傻,这个问题可能最好被删除,因为经过几个小时的拔毛后我在发布问题后10秒就解决了。

问题出在我的DependencyProperty.Register调用中。我有所有者类型(第三个参数)引用一个名称非常相似的类,而不是实际拥有的类。哎呀。 :(