A 'Binding' can only be set on a DependencyProperty of a DependencyObject

后端 未结 6 1310
陌清茗
陌清茗 2020-12-03 02:39

From a custom control based on TextBox, I created a property named Items, in this way:

public class NewTextBox : TextBox
{
    publ         


        
6条回答
  •  眼角桃花
    2020-12-03 03:04

    To solve this exception you need to change the property Items and add a DependencyProperty that will work as a "link" in XAML. The class will be:

    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...
        }
    

提交回复
热议问题