Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an event that fires when a binding has been set on a property?

Tags:

c#

.net

wpf

xaml

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?

like image 789
JoshG Avatar asked Jun 26 '12 08:06

JoshG


People also ask

How to implement INotifyPropertyChanged?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

What is OnPropertyChanged?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .


3 Answers

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.

like image 69
Isak Savo Avatar answered Sep 28 '22 16:09

Isak Savo


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


        }
like image 24
JohnnBlade Avatar answered Sep 28 '22 16:09

JohnnBlade


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}" />
like image 28
Dean Chalk Avatar answered Sep 28 '22 17:09

Dean Chalk