2014-02-11 178 views
1

我在WPF中創建了一個UserControl並生成了一些依賴屬性。 但在其中一個屬性中,我無法在XAML中設置綁定。WPF自定義用戶控件 - 依賴屬性綁定

internal Visibility ProgressbarVisibility 
{ 
    get { return (Visibility)GetValue(ProgressbarVisibilityProperty); } 
    set { SetValue(ProgressbarVisibilityProperty, value); } 
} 

internal static readonly DependencyProperty ProgressbarVisibilityProperty = 
      DependencyProperty.Register("ProgressbarVisibility", typeof(Visibility), typeof(ImportBox), new PropertyMetadata(Visibility.Hidden)); 

,所以我得到了以下錯誤:

A 'Binding' cannot be set on the 'ProgressbarVisibility' property of type 'ImportBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

當我設置屬性「硬編碼」一個固定值,它沒有問題。

其他Dependeny屬性不拋出這種類型的錯誤,我可以綁定everthing我想要的。

internal ImageSource ImageSource 
     { 
     get { return (ImageSource)GetValue(ImageSourceProperty); } 
     set { SetValue(ImageSourceProperty, value); } 
     } 

internal static readonly DependencyProperty ImageSourceProperty = 
      DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImportBox)); 

internal string HeaderText 
     { 
     get { return (string)GetValue(HeaderTextProperty); } 
     set { SetValue(HeaderTextProperty, value); } 
     } 

internal static readonly DependencyProperty HeaderTextProperty = 
      DependencyProperty.Register("HeaderText", typeof(string), typeof(ImportBox)); 

internal UIElement PresenterContent 
     { 
     get { return (UIElement)GetValue(PresenterContentProperty); } 
     set { SetValue(PresenterContentProperty, value); } 
     } 

internal static readonly DependencyProperty PresenterContentProperty = 
      DependencyProperty.Register("PresenterContent", typeof(UIElement), typeof(ImportBox)); 

回答

2

充分利用DependencyProperty爲公衆將解決您的問題...

public Visibility ProgressbarVisibility 
{ 
    get { return (Visibility)GetValue(ProgressbarVisibilityProperty); } 
    set { SetValue(ProgressbarVisibilityProperty, value); } 
} 

public static readonly DependencyProperty ProgressbarVisibilityProperty = 
     DependencyProperty.Register("ProgressbarVisibility", typeof(Visibility), typeof(ImportBox), new PropertyMetadata(Visibility.Hidden)); 
+1

好了...謝謝!第二個問題是,我在DataContext中設置屬性也是內部的。現在它的作品。 – Hiead

相關問題