Attached Property: Check Binding

Short question

Is there a quick way to find out what a particular attached property is bound to at runtime?

Detail

I am debugging a UserControl (which inherits ItemsControl ), which associates Canvas.Left and Canvas.Top its elements with two properties of ViewModel objects via style. At run time, I place a breakpoint in a specific place and want to check the binding of the attached Canvas.Left property.

Note that I do not want to see the current value of the attached property for the element. I can easily do this by checking the value of Canvas.GetLeft(myItem) in QuickWatch or Immediate windows. I want to check the actual binding here, that is, the name of the VM property to which this attached property for myItem .

I already tried Snoop, which, unfortunately, does not show bindings of attached properties (unless I missed anything obvious).

0
c # data-binding wpf xaml attached-properties
source share
2 answers

Is there a quick way to find out what a particular attached property is tied to at runtime

Yes, just override the attached property somewhere (in your window?):

 <Window x:Class="WpfApplication1.MainWindow" ... > <Grid Canvas.Top="123"/> </Window> 

and code

 public MainWindow() { InitializeComponent(); Canvas.TopProperty.OverrideMetadata(typeof(MainWindow), new FrameworkPropertyMetadata((d, e) => { // you will get here for each Canvas.Top set in MainWindow MessageBox.Show(d.ToString()); })); } 
0
source share

You can bind a binding binding to software in the same way as normal binding of dependency properties. That is, from the code behind, to get the bound binding of the Canvas.LeftProperty property of the control named myItemsControl :

 BindingExpression bindingExpression = myItemsControl.GetBindingExpression(Canvas.LeftProperty); Binding parentBinding = bindingExpression.ParentBinding; 
0
source share

All Articles