Debugging Data Binding in WPF

Source: Internet
Author: User

Original post: http://jimmangaly.blogspot.com/2009/04/debugging-data-binding-in-wpf.html

 

What do you do when you suspect that a data binding is broken in your WPF application? Looking at the Output window for data binding exceptions can be helpful in login cases. However, there are cases where this does not help.

For example, below I have TextBox. Text bound to Slider. Value. But notice that the binding is OneWay.

<TextBox Text = "{Binding ElementName = slider, Path = Value, Mode = OneWay}" Height = "30" Width = "100" Margin = "10"/>

<Slider Minimum = "0" Maximum = "100" Value = "20" Margin = "10" x: Name = "slider"/>

When I move the slider, the text in the TextBox shows the slider value as expected. now I enter some text in the TextBox (which causes the binding to break since it is OneWay binding; but assume that I haven't realized that ). I move the slider now, but the text in TextBox does not update. I suspect that the binding is broken. but how do I confirm that?

Simply looking at the Output Window in this case reveals nothing. what I 've been doing until now is to add a dummy converter and set a break point in the Convert method. if the break point is not hit I know that the binding is broken. but that is really a long way of doing something that shoshould be really done in a simpler way ....

WPF 3.5 has a new property called PresentationTraceSources. traceLevel that you can set on your data binding to get more detailed information on the binding in the Output Window. in fact, if you set the trace level to High you can get details on every step in the process of the binding.

So to the above code, I make two changes

  • Add a namespace reference to System. Diagnostics
  • Set PresentationTraceSources. TraceLevel = High on the binding I want to inspect.

<Window x: Class = "DebugBindings. Window1"... xmlns: trace = "clr-namespace: System. Diagnostics; assembly = WindowsBase">

<StackPanel>

<TextBox Text = "{Binding ElementName = slider, Path = Value, Mode = OneWay, trace: PresentationTraceSources. traceLevel = High} "Height =" 30 "Width =" 100 "Margin =" 10 "/>

<Slider Minimum = "0" Maximum = "100" Value = "20" Margin = "10" x: Name = "slider"/>

StackPanel>

Window>

Now when I inspect the Output Window after running the application, I can see step-by-step details of the data binding process.

Created BindingExpression (hash = 17654054) for Binding (hash = 44624228)
Path: 'value'
BindingExpression (hash = 17654054): Default update trigger resolved to LostFocus
BindingExpression (hash = 17654054): Attach to System. Windows. Controls. TextBox. Text (hash = 52727599)
BindingExpression (hash = 17654054): Resolving source
BindingExpression (hash = 17654054): Found data context element: (OK)
Lookup name slider: queried TextBox (hash = 52727599)
BindingExpression (hash = 17654054): Resolve source deferred

BindingExpression (hash = 17654054): Resolving source
BindingExpression (hash = 17654054): Found data context element: (OK)
Lookup name slider: queried TextBox (hash = 52727599)
BindingExpression (hash = 17654054): Activate with root item Slider (hash = 54371668)
BindingExpression (hash = 17654054): At level 0-for Slider. Value found accessor DependencyProperty (Value)
BindingExpression (hash = 17654054): Replace item at level 0 with Slider (hash = 54371668), using accessor DependencyProperty (Value)
BindingExpression (hash = 17654054): GetValue at level 0 from Slider (hash = 54371668) using DependencyProperty (Value): '20'
BindingExpression (hash = 17654054): TransferValue-got raw value '20'
BindingExpression (hash = 17654054): TransferValue-implicit converter produced '20'
BindingExpression (hash = 17654054): TransferValue-using final value '20'

If I now enter text in the TextBox, I can check the Output Window to confirm that the binding was indeed broken!

BindingExpression (hash = 17654054): Deactivate
BindingExpression (hash = 17654054): Replace item at level 0 with {NullDataItem}
BindingExpression (hash = 17654054): Detach

The only down side is that you have to manually add this property to each binding that you want to inspect.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.