Hamid
Hamid

Reputation: 1099

GetBindingExpression returns null in the Loaded event

I'm trying to call the GetBindingExpression method in the Loaded event, but it always returns null.

Is this expected behavior, or am I doing something wrong? If it is expected, after what event do binding expressions become available?

I just create custom control

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

And use this control in xaml

<Controls:LookUp Name="cb1"  LookUpItemsSource="{x:Static Helper:DataManager.CycleLookUpData}"

Now i want to get binding expression when control initialized that method return null:

cb1.GetBindingExpression(LookUp.LookUpItemsSourceProperty)

Upvotes: 0

Views: 1447

Answers (2)

zmechanic
zmechanic

Reputation: 1990

If you don't use {Binding ... in XAML you can't use GetBindingExpression() method. In your case you set value instead of binding. You need to use cb1.GetValue(LookUp.LookUpItemsSourceProperty) instead.

Upvotes: 1

Akash Kava
Akash Kava

Reputation: 39916

x:static will set the value of key, it is not binding expression. You will have to use,

{Binding CycleLookUpData, source={x:static Helper:DataManager}}

Upvotes: 1

Related Questions