Joe
Joe

Reputation: 6796

How can I put a breakpoint on WPF DependencyProperty retrieval?

How can I create a breakpoint triggered when when WPF is retrieving the value of my custom DependencyProperty?

I wrote this question poorly the first time. Unfortunately, understanding why my breakpoint is not hit doesn't solve my real problem. This is the question I should have asked. Searching high and low I still can't see how to do this.

If WPF is going to bypass my getter function, is there some other way?

I need this for debugging purposes, to examine the state of my control live in the debugger at the exact moment WPF retrieves this property.

To refresh, below is my custom dependency property

private static readonly DependencyPropertyKey ColorizerPropertyKey =
    DependencyProperty.RegisterReadOnly(
        nameof(Colorizer),
        typeof(SurfaceSeries3DColorizer),
        typeof(SurfaceDisplay),
        new FrameworkPropertyMetadata());

private static readonly DependencyProperty ColorizerProperty = 
    ColorizerPropertyKey.DependencyProperty;

public SurfaceSeries3DColorizer Colorizer
{
    get => (SurfaceSeries3DColorizer) GetValue(ColorizerProperty);
    private set => SetValue(ColorizerPropertyKey, value);
}

...and this is the XAML that binds to it

<tk:SurfaceSeries3D ItemsSource="{Binding Points}"
                    XValueBinding="X"
                    YValueBinding="Y"
                    ZValueBinding="Z"
                    Colorizer="{Binding Colorizer}"
                    />

Upvotes: 2

Views: 1313

Answers (1)

dymanoid
dymanoid

Reputation: 15197

There is a way for that, but you have to keep in mind that your debuggee (the application you debug) and the debugger itself will become very unresponsive.

So if you're trying to diagnose some nasty and rare occurring bug, this might help. Otherwise, it is pretty useless, because your app will be almost unusable.

Go to the Breakpoints window in Visual Studio and choose New --> Function Breakpoint....

Type in:

System.Windows.DependencyObject.GetValue

Enable the Condition checkbox and enter your condition:

dp == YourNamespace.YourClass.YourDependencyProperty

Here, dp is the GetValue method's argument name (don't change) and YourNamespace.YourClass.YourDependencyProperty is the full name of your dependency property public field (change).

Now, hit F5 and enjoy.

Behind the scenes, the debugger will check each and every call to the System.Windows.DependencyObject.GetValue method, and when the provided argument will be your property, the debugger will break your app.

Note that WPF calls that method very frequently - that's why your app and the debugger will become unresponsive: a condition will be checked on each call, and that condition requires data transfer between debugger and debuggee.

Upvotes: 3

Related Questions