4

Our Silverlight app contains a third-party control, which contains some ScrollBars (among other things). In order to troubleshoot a problem, I want to be able to stop in the debugger whenever the third-party control modifies the Minimum or Maximum properties of any of its scrollbars. Then I'll be able to look at the stack trace and learn more about what's going on.

If I was interested in the ScrollBars' Value property, that would all be easy -- ScrollBar has a ValueChanged event, so I could just add some throwaway code that hooks that event on the ScrollBar, set a breakpoint inside my event handler, and debug away. But there are no corresponding CLR events for MinimumChanged or MaximumChanged, so it won't be that simple.

I ran across a blog post that talks about using DependencyPropertyDescriptor to get dependency property change events, but unfortunately, DependencyPropertyDescriptor doesn't exist in Silverlight.

How can I get to the point where I can set a breakpoint that fires whenever the ScrollBar's Minimum and Maximum properties change?

Joe White
  • 94,807
  • 60
  • 220
  • 330

1 Answers1

3

The following idea springs to my mind:

  • Create a user control with a dependency property. (The XAML within the user control won't actually be used, we just need the dependency property.)
  • Bind the user control's dependency property to the Minimum or Maximum property of the third party control (assuming they are also dependency properties).
  • In the code-behind of the user control, add a PropertyChangedCallback to the dependency property and drop a breakpoint in that.

This approach should allow you to set a breakpoint that fires whenever the Minimum or Maximum property changes. However, I can't guarantee that you'll get a stacktrace that will help you.

The code-behind of the user control could look something like this:

public partial class DPContainer : UserControl
{
    public static readonly DependencyProperty DebugValueProperty =
        DependencyProperty.Register("DebugValue", typeof(object), typeof(DPContainer), new PropertyMetadata(DebugValue_Changed));

    public DPContainer()
    {
        InitializeComponent();
    }

    public object DebugValue
    {
        get { return GetValue(DebugValueProperty); }
        set { SetValue(DebugValueProperty, value); }
    }

    private static void DebugValue_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        // Drop a breakpoint in this method.
    }

Assuming that you have a ScrollBar with x:Name="someScrollBar", you could then add something like the following to your XAML:

<local:DPContainer DebugValue="{Binding Path=Minimum, ElementName=someScrollBar}" />
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • The scrollbar is part of the template of a third-party control, so I can't bind to it with ElementName. I might be able to create the binding in code, though. – Joe White Sep 16 '11 at 20:14