Attached property: Check binding
Asked Answered
P

2

2

Short question

Is there a quick way of knowing what a particular attached property is bound to, at runtime?

Detail

I'm debugging a UserControl (that inherits ItemsControl) which binds Canvas.Left and Canvas.Top of its items to two properties of the ViewModel objects, through a style. At runtime, I place a breakpoint at a certain location and want to inspect the binding of Canvas.Left attached property.

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

I have tried Snoop already, which unfortunately doesn't show bindings of attached properties (if I didn't miss something obvious).

Procathedral answered 6/8, 2015 at 13:32 Comment(5)
Output window will show you any binding errors. No errors could mean what binding (if there is really any) was successful. Attached property has callback when value is changed, you can use that to debug attached property binding.Dollydolman
https://mcmap.net/q/1389488/-wpf-how-do-i-get-an-object-that-is-bound-to-a-listboxitem-back this link may helpDiazomethane
@NeverAgain: That post doesn't talk about attached properties.Procathedral
@Sinatr: I do not see any binding errors in output window. Plus the value of the attached property is not changing, so listening to the callback won't help.Procathedral
Event is rised at least once after loading baml (e.g. if you wrote in xaml Canvas.Top="0" you will get it, if you don't write, then it has default value obviously), you can override property, see here, then set callback and see what it was set for a given item (sender will be the DependencyProperty for which you set attached property value).Dollydolman
D
0

Is there a quick way of knowing what a particular attached property is bound to, at runtime

Yes, just override 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());
    }));
}
Dollydolman answered 6/8, 2015 at 14:2 Comment(0)
W
0

You can get the attached property binding programmatically the same way as you get normal dependency property binding. I.e. from code behind to get the Canvas.LeftProperty attached property binding of the control with the name myItemsControl:

BindingExpression bindingExpression = myItemsControl.GetBindingExpression(Canvas.LeftProperty);
Binding parentBinding = bindingExpression.ParentBinding;
Workingwoman answered 27/11, 2016 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.