RLH
RLH

Reputation: 15698

How can I access the binding properties of a XAML object, from my code behind?

I would like to access the binding settings for a control in a XAML document, from within my C# code. Specifically, I'd like to set a breakpoint in my code and using the Visual Studio's Locals window, I'd like to inspect the properties associated with a bound control, from within the VS debugger.

Can this be done?

In my case I have a TextBox with binding defined in my XAML file. I have a StringFormat specified in my binding statement and I'd like to see where the string formatter details are stored within my TextBox. After setting a breakpoint, and inspecting the TextBox for a while, I cannot find the underlying binding properties?!

I assume there has to be a way for me to be able to access my binding settings from my ViewModel code. Where are these binding settings stored?

NOTE: I know that the DataContext contains the data object that my control is interacting upon. However, that is not what I need. I need to see, and inspect, the settings that wire the data object with the XAML UI object.

Upvotes: 2

Views: 965

Answers (1)

Ayyappan Subramanian
Ayyappan Subramanian

Reputation: 5366

You can access from your code behind using below code.

BindingExpression be= txt.GetBindingExpression(TextBox.TextProperty);
        string format=be.ParentBinding.StringFormat;

Upvotes: 3

Related Questions