10

I've got a problem where I need to know about all the bindings that have been made to the dependency properties of my object. Currently, I am iterating over the dependency properties whenever my datacontext changes, and looking for binding expressions. But I have discovered that in some cases (TabControls), the data context appears to be set first, then the bindings from XAML applied.

So, is there a way that I can detect a binding being applied to one of my dependency properties?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
JoshG
  • 775
  • 1
  • 7
  • 19
  • Its a bit complicated to explain, but it is basically so the object that is the data context (a helper class to help visualise the data of another class) knows that this control is presenting an interface to edit the object that is stored in the property that is specified in the binding expression path. – JoshG Jun 26 '12 at 08:44

4 Answers4

8

Assuming you are inside a UserControl, you should be able to use the Loaded event for this. That event is fired when "the element is laid out, rendered, and ready for interaction." I can only assume this means that bindings have been completed.

You could then, in the Loaded event handler just tell your datacontext that you are binding to it.

If you expect the datacontext to change, you'll need to combine this with a DataContextChanged event handler as well.

Isak Savo
  • 34,957
  • 11
  • 60
  • 92
  • Thanks for understanding my question. This is indeed what I was looking for! I've just checked and your assumption appears to hold up. Thanks – JoshG Jun 27 '12 at 00:26
  • 2
    Actually, `Loaded` and `Unloaded` are called whenever the element is loaded/unloaded from the visual tree, and it may happen many times during the lifetime of that element (e.g. theme change can cause the element to reload). – Grx70 May 21 '16 at 06:34
  • You are of course correct @Grx70 - I was young an inexperienced when I wrote this answer ;-) I've removed that sentence now. – Isak Savo May 21 '16 at 19:55
  • Is it bad practice to use a DataContextChanged event when a Loaded event handler may suffice? – rollsch Nov 16 '16 at 23:29
2

I assume that yr using the private static DataContextChanged event to know when yr datacontext changes right

here is some of my code This is what i do

 public static readonly DependencyProperty ApplicationDataContextProperty =
            DependencyProperty.Register("ApplicationDataContext",
            typeof(Object),
            typeof(MyControl),
            new PropertyMetadata(MyControl_DataContextChanged));

// my constructor

        public MyControl()
        {

                InitializeComponent();

                if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
                {
                    SetBinding(ApplicationDataContextProperty, new Binding());
                }

        }

// my event
        private static void MyControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

                MyControl thisControl = sender as MyControl
                if (thisControl != null)
                {
                    INotifyPropertyChanged propertyChanged;
                    propertyChanged = e.OldValue as INotifyPropertyChanged;
                    if (propertyChanged != null)
                        propertyChanged.PropertyChanged -= thisControl.propertyChanged_PropertyChanged;


                    propertyChanged = e.NewValue as INotifyPropertyChanged;
                    if (propertyChanged != null)
                        propertyChanged.PropertyChanged += thisControl.propertyChanged_PropertyChanged;
                }

        }

// my 2e event
        void propertyChanged_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {

                if (e.PropertyName == "ListWithUsers")
                    LoadGrid();


        }
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
  • Yeah, I'm using DataContextChanged += event handler. I do this before InitializeComponent() in the constructor of my control. Inside OnDataContextChanged, it returns null for binding expressions that are definitely bound (the binding works visually). But obviously haven't been assigned yet. – JoshG Jun 26 '12 at 08:37
  • If I simply change the data context, then because the control has already been initialised, then the binding expressions return perfectly. – JoshG Jun 26 '12 at 08:38
  • Hmm... How does knowing the data context has changed help me detect when a binding has been applied to my object? – JoshG Jun 26 '12 at 11:54
  • JoshG Because when a binding has been applied the data context will change. – rollsch Nov 16 '16 at 23:32
1

try using NotifyOnSourceUpdated on critical bindings

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.notifyonsourceupdated.aspx

or alternatively you can get detailed binding information in your output window by using PresentationTraceSources

for example

<TextBlock Text="{Binding Name, PresentationTraceSources.TraceLevel=High}" />
Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
-1

Raising an event when a property changes is precisely what INotifyPropertyChanged does. There's one required member to implement INotifyPropertyChanged and that is the PropertyChanged event

Sample and more details : Raise an event whenever a property's value changed?

Community
  • 1
  • 1
Ponmalar
  • 6,871
  • 10
  • 50
  • 80
  • I don't want to know when the property changes. I want to know when a binding has been connected to the property. I don't believe your solution does this? The property may not have changed by connecting a binding. i.e value = null, with binding, value still = null. – JoshG Jun 26 '12 at 08:30