简体   繁体   English

WPF自定义UserControl-依赖属性绑定

[英]WPF Custom UserControl - Dependency Property Binding

I created a UserControl in WPF and generated some Dependency Properties. 我在WPF中创建了一个UserControl并生成了一些依赖项属性。 But on one of the Properties i cannot set a Binding in XAML. 但是在属性之一上,我无法在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));

So i get the following Error: 所以我得到以下错误:

A 'Binding' cannot be set on the 'ProgressbarVisibility' property of type 'ImportBox'. 不能在“ ImportBox”类型的“ ProgressbarVisibility”属性上设置“ Binding”。 A 'Binding' can only be set on a DependencyProperty of a DependencyObject. 只能在DependencyObject的DependencyProperty上设置“绑定”。

When i set the Property "hardcoded" with a fix Value, its no Problem. 当我将属性“固定编码”设置为固定值时,这没有问题。

The other Dependeny Properties dont throw Errors of this type and i can Bind everthing i want. 其他Dependeny Properties不会抛出这种类型的错误,我可以绑定我想要的所有内容。

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

Make the DependencyProperty as Public will solves your problem... 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM