2014-02-26 111 views
0

我很難讓依賴屬性工作而不會導致編譯錯誤。WPF DependencyProperty用法

我有一個名爲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} 

usercontrol在沒有DP的情況下完美工作。我究竟做錯了什麼?

奇怪的是,當我真的把Title =「...」屬性放到xaml中時,它在設計器中工作,直到我嘗試編譯它爲止。從那時起,它給了我提到的錯誤。

回答

2

DependencyPropertystring但在PropertyMetadata指定爲假

+0

我真的很感激它,如果你可以回答一個後續問題:http://stackoverflow.com/questions/22057177/wpf-dependancy-control-updates – Shaku

2

當屬性類型爲string時,您已將默認值指定爲bool

變化

new PropertyMetadata(false) 

new PropertyMetadata(null) 
+0

哦,我瞎了默認值。謝謝! – Shaku

相關問題