patrick
patrick

Reputation: 16949

What's wrong with my Dependency Property?

The compiler and the runtime don't complain about the binding, but the Setter is never hit in my AutoCompleteTextBox.

<Controls:AutoCompleteTextBox   
    Items="{Binding Path=DropDownValues}"
    Width="200" 
    Grid.Column="1" 
    Height="30"
   Tag="{Binding}"
  />

And

 public partial class AutoCompleteTextBox
    {
        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register(
                "Items",
                typeof(ItemCollection),
                typeof(AutoCompleteTextBox),
                new PropertyMetadata(default(ItemCollection), OnItemsPropertyChanged));


       public ItemCollection Items
       {
           get
           {
               return (ItemCollection)GetValue(ItemsProperty);
           }
           set
           {
               SetValue(ItemsProperty, value); //doesn't get hit
           }
       }


//This is how i'm cheating since my Items is always null
        private void CanvasName_Loaded(object sender, RoutedEventArgs e)
        {
            object obj = this.Tag;

            if (obj != null)
            {
                CjisQueryAutoCompleteData at = obj as CjisQueryAutoCompleteData;
                if (at != null)
                {
                    //use the data...
                    PopDropDown(at.DropDownValues);
                }
            }
        }

       //....
    }

Upvotes: 2

Views: 190

Answers (1)

Clemens
Clemens

Reputation: 128013

There's nothing wrong. When a dependency property is set in XAML, WPF directly accesses the DependencyProperty without calling the CLR wrapper. Think of it as if WPF would directly call SetValue.

See XAML Loading and Dependency Properties, Implications for Custom Dependency Properties.

You will however notice that the property has been set when your OnItemsPropertyChanged callback is called.

Upvotes: 9

Related Questions