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?
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.
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 .
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}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With