【问题标题】:WPF Custom UserControl - Dependency Property BindingWPF 自定义 UserControl - 依赖属性绑定
【发布时间】:2014-02-11 08:15:14
【问题描述】:

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

所以我得到以下错误:

不能在“ProgressbarVisibility”属性上设置“绑定” 输入“进口箱”。只能在 DependencyProperty 上设置“绑定” 一个 DependencyObject。

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

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

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

【问题讨论】:

    标签: wpf xaml binding user-controls dependency-properties


    【解决方案1】:

    DependencyProperty 设为 Public 将解决您的问题...

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

    【讨论】:

    • 好的...谢谢!第二个问题是,我也在内部设置了 Datacontext 中的属性。现在它的作品。
    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2013-10-16
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多