简体   繁体   中英

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

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?

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.

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


        }

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}" />

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM