WPF 自定义用户控件 - 依赖项属性绑定



我在WPF中创建了一个UserControl并生成了一些依赖项属性。但是在其中一个属性上,我无法在 XAML 中设置绑定。

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));

所以我收到以下错误:

不能在 的 "进度条可见性" 属性上设置"绑定" 键入"导入框"。只能在依赖项属性上设置"绑定" 的依赖对象。

当我使用固定值设置属性"硬编码"时,它没有问题。

其他依赖项属性不会抛出这种类型的错误,我可以绑定我想要的任何内容。

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));

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));

最新更新