2012-07-11 103 views
31

的一個DependencyProperty設置基於TextBox自定義控件,我創建了一個名爲Items屬性,以這樣的方式A「綁定」只能在DependencyObject的

public class NewTextBox : TextBox 
{ 
    public ItemCollection Items { get; set; } 
} 

當使用XAML自定義控件,我無法綁定屬性,因爲它引發異常「A'綁定'只能在DependencyObject的DependencyProperty上設置。」

如何解決此異常?

+2

是的。只有依賴屬性可以充當綁定的目標。源可能是一個依賴項屬性或實現INotifyPropertyChanged的CLR屬性 – Gishu 2012-07-11 15:16:13

+0

這是您的其他問題的確切副本,您接受答案並說「但我必須修改屬性以包含DependencyProperty」。你的解決方案應該被包含在那裏作爲答案 – arserbin3 2012-07-11 15:38:07

+5

@AdamHouldsworth是的,這個問題是張貼只是爲了張貼答案。這實際上被鼓勵,因爲它被看作是一種共享知識的形式,甚至還有一個[在Ask Question表單上的新的CheckBox](http://meta.stackexchange.com/questions/132886/what-is-這回答你自己的問題爵士樂),這將允許你在你寫問題的同時寫一個答案。 – Rachel 2012-07-11 15:56:36

回答

20

要解決此異常,您需要更改屬性Items並添加一個DependencyProperty,它將用作XAML中的「鏈接」。本課程將是:

public class AutocompleteTextBox : TextBox 
{ 
    public ItemCollection Items 
    { 
     get { 
      return (ItemCollection)GetValue(ItemsProperty); } 
     set { 
      SetValue(ItemsProperty, value); } 
    } 

    public static readonly DependencyProperty ItemsProperty = 
     DependencyProperty.Register(
      "Items", 
      typeof(ItemCollection), 
      typeof(AutocompleteTextBox), 
      new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged)); 

    private static void OnItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     // AutocompleteTextBox source = d as AutocompleteTextBox; 
     // Do something... 
    } 
53

作爲一個側面說明,這也是值得注意的是,如果你複製你會得到這些綁定錯誤和對象之間的粘貼和忘記改變第二typeof(Object)聲明。

我找不到一個好時辰,爲什麼我得到這個錯誤,因爲一切似乎都被定義和正確。我想將我的屬性移動到用戶控件中,因爲我想從一個集合到一個列表。因此:

public static readonly DependencyProperty FoldersProperty = DependencyProperty.Register("Folders", typeof(OutlookFolders), typeof(MainWindow), new FrameworkPropertyMetadata(new OutlookFolders())); 

public OutlookFolders Folders 
{ 
    get { return GetValue(FoldersProperty) as OutlookFolders; } 
    set { SetValue(FoldersProperty, value); } 
} 

應該成爲:

public static readonly DependencyProperty FoldersProperty = DependencyProperty.Register("Folders", typeof(OutlookFolders), typeof(SavedFolderControl), new FrameworkPropertyMetadata(new OutlookFolders())); 

public OutlookFolders Folders 
{ 
    get { return GetValue(FoldersProperty) as OutlookFolders; } 
    set { SetValue(FoldersProperty, value); } 
} 

直到我做了這個改變我不停收到錯誤: A 'Binding' cannot be set on the property 'Folders' of type 'SavedFolderControl'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

+3

之前的複選框。我用一些DependencyProperties複製了UserControl,這意味着舊的''typeof(____)''-Value仍然存在並且是有效的。直到運行時纔會出現任何錯誤。然後,XAML解析異常「綁定只能設置...」等等。謝謝! – ecth 2016-08-02 07:22:34

+0

它可能存在,但點建議類型實際匹配的類型。 – netniV 2016-08-02 07:24:52

+0

不,這就是要點。我的意思是這門課存在並且是有效的。所以你不會在VS中發生任何錯誤。直到你編譯並運行。但是你不知道錯誤來自哪裏!這是我複製後的薄霧。 – ecth 2016-08-02 11:02:13

3

這樣做的另一個潛在原因是,當你提供一個壞的類型元數據中的默認值。
例如:

new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged) 

,如果你寫的,而不是將拋出這個錯誤:

new PropertyMetadata(false, OnItemsPropertyChanged) 

這也有可能發生,如果你是從源代碼複製和粘貼。

16

下面是另一個問題:確保DependencyProperty.Register()的第一個參數中的字符串與相關屬性的名稱匹配。

public static readonly DependencyProperty ItemsProperty = 
    DependencyProperty.Register(
     "TheItems", // This is wrong 
     typeof(ItemCollection), 
     typeof(AutocompleteTextBox), 
     new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged)); 

我遇到了這個問題,當我重命名我的屬性而不改變字符串。

+4

確保名稱是「Items」,而不是「ItemsProperty」 – 2015-05-13 17:51:13

+7

您可以在最新.NET版本中使用nameof(Items)以避免此問題。 – Herman 2016-02-18 09:25:57

0

我有(運行時間+設計時)消息:

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: A 'Binding' cannot be set on the 'Property' property of type 'Trigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

在那裏我是足夠聰明的定義在虛擬機屬性觸發..

// incorrect.. canont have Trigger for VM property 
<Trigger Property="{Binding IsExpanded}" Value="True"> 
    <Setter Property="Visibility" Value="Visible"/> 
</Trigger> 

哪些應該ofcourse是datatrigger(它使用綁定而不是屬性)

<dataTrigger Binding="{Binding IsExpanded}" Value="True"> 
    <Setter Property="Visibility" Value="Visible"/> 
</Trigger> 

觸發器通常用於控件的(Button,TextBox ,FrameworkElement等)屬性。

相關問題