我的依赖属性有什么问题?

时间:2012-12-21 21:57:06

标签: c# wpf xaml

编译器和运行时不会抱怨绑定,但是在我的AutoCompleteTextBox中永远不会遇到Setter。

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

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

       //....
    }

1 个答案:

答案 0 :(得分:9)

没有错。在XAML中设置依赖项属性时,WPF直接访问DependencyProperty而不调用CLR包装器。可以把它想象成WPF会直接调用SetValue。

XAML Loading and Dependency Properties, 自定义依赖属性的含义。

但是,您会注意到在调用OnItemsPropertyChanged回调时已设置该属性。