我试图在Loaded事件中调用GetBindingExpression方法,但它总是返回null
。
这是意料之中的行为,还是我做错了什么?如果是预期的,那么在什么事件之后绑定表达式才可用?
我只是创建自定义控制
public partial class LookUp : ComboBox
public static readonly DependencyProperty LookUpItemsSourceProperty =
DependencyProperty.Register("LookUpItemsSource"
, typeof(IEnumerable)
, typeof(LookUp)
, new PropertyMetadata(OnItemsSourcePropertyChanged));
public IEnumerable LookUpItemsSource
{
get
{
return this.GetValue(LookUpItemsSourceProperty) as IEnumerable;
}
set
{
this.SetValue(LookUpItemsSourceProperty, value);
}
}
并在xaml 中使用此控件
<Controls:LookUp Name="cb1" LookUpItemsSource="{x:Static Helper:DataManager.CycleLookUpData}"
现在我想在控件初始化时得到绑定表达式,该方法返回null:
cb1.GetBindingExpression(LookUp.LookUpItemsSourceProperty)
x:static将设置key的值,它不是绑定表达式。你必须使用
{Binding CycleLookUpData, source={x:static Helper:DataManager}}
如果在XAML中不使用{Binding ...
,则不能使用GetBindingExpression()
方法。在您的情况下,您设置值而不是绑定。您需要使用cb1.GetValue(LookUp.LookUpItemsSourceProperty)
。