简体   繁体   English

在属性上设置绑定时是否会触发事件?

[英]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. 目前,每当我的datacontext发生更改时,我都会迭代依赖项属性,并寻找绑定表达式。 But I have discovered that in some cases (TabControls), the data context appears to be set first, then the bindings from XAML applied. 但我发现在某些情况下(TabControls),数据上下文似乎首先被设置,然后应用XAML的绑定。

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. 假设您在UserControl中,您应该能够使用Loaded事件。 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. 然后,您可以在Loaded事件处理程序中告诉您的datacontext您绑定它。

If you expect the datacontext to change, you'll need to combine this with a DataContextChanged event handler as well. 如果您希望更改datacontext,则还需要将其与DataContextChanged事件处理程序结合使用。

I assume that yr using the private static DataContextChanged event to know when yr datacontext changes right 我假设您使用私有静态DataContextChanged事件来了解yal datacontext何时更改

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 尝试在关键绑定上使用NotifyOnSourceUpdated

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.notifyonsourceupdated.aspx 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 或者,您可以使用PresentationTraceSources在输出窗口中获取详细的绑定信息

for example 例如

<TextBlock Text="{Binding Name, PresentationTraceSources.TraceLevel=High}" />

Raising an event when a property changes is precisely what INotifyPropertyChanged does. 在属性更改时引发事件正是INotifyPropertyChanged所做的事情。 There's one required member to implement INotifyPropertyChanged and that is the PropertyChanged event 有一个必需的成员来实现INotifyPropertyChanged,那就是PropertyChanged事件

Sample and more details : Raise an event whenever a property's value changed? 示例和更多详细信息: 每当属性值发生变化时都会引发事件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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