编译器和运行时不会抱怨绑定,但是在我的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);
}
}
}
//....
}
答案 0 :(得分:9)
没有错。在XAML中设置依赖项属性时,WPF直接访问DependencyProperty而不调用CLR包装器。可以把它想象成WPF会直接调用SetValue。
见XAML Loading and Dependency Properties, 自定义依赖属性的含义。
但是,您会注意到在调用OnItemsPropertyChanged
回调时已设置该属性。