Posts
17
Comments
111
Trackbacks
2048
Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5

Debugging WPF databinding problems can be sometimes a painful experience: consider this simple XAML fragment and its codebehind that doesn’t work due to mistakenly typed CustomerName inside binding expression.

<Window x:Class="WPF_Databind.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml             
    Title="Window1" Height="300" Width="00">
    <Grid>
        <TextBox 
            Text="{Binding Path=CustomerName}"
            Height="23" Margin="87,65,71,0" Name="textBox1" VerticalAlignment="Top" />
    </Grid>
</Window>

 

namespace WPF_Databind { public partial class Window1 : Window { public Window1 () { InitializeComponent(); this.DataContext = new Customer() { Name = "Foo", Id = 42 }; } } public class Customer { public string Name { get; set; } public int Id { get; set; } } }

Running this sample results in an empty Textbox: how do we get hints about what’s wrong with it? , the only place where we can look for a possible reason is Visual Studio output window (see below) and among various output messages search for some useful information like:

System.Windows.Data Error: 35 : BindingExpression path error: 'CustomerName' property not found on 'object' ''Customer' (HashCode=22613453)'. BindingExpression:Path=CustomerName; DataItem='Customer' (HashCode=22613453); target element is 'TextBox' (Name='textBox1'); target property is 'Text' (type 'String')

In some cases this wouldn’t help you much and you’d like to have more details about what’s wrong with your databinding, well starting from .NET Framework 3.5 you can thanks to PresentationTraceSources class.
Let’s modify our previous sample importing System.Diagnostics namespace (that’s where PresentationTraceSources lives even if hosted inside WindowsBase assembly) and applying it to our failing binding.

<Window x:Class="WPF_Databind.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"                
    Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBox 
            Text="{Binding Path=CustomerName,diag:PresentationTraceSources.TraceLevel=High}"
            Height="23" Margin="87,65,71,0" Name="textBox1" VerticalAlignment="Top" />
    </Grid>
</Window>

Running sample again we can see a more verbose description of what really happens behind our binding:

System.Windows.Data Warning: 47 : Created BindingExpression (hash=14421545) for Binding (hash=33163964)
System.Windows.Data Warning: 49 : Path: 'CustomerName'
System.Windows.Data Warning: 51 : BindingExpression (hash=14421545): Default mode resolved to TwoWay
System.Windows.Data Warning: 52 : BindingExpression (hash=14421545): Default update trigger resolved to LostFocus
System.Windows.Data Warning: 53 : BindingExpression (hash=14421545): Attach to System.Windows.Controls.TextBox.Text (hash=35567111)System.Windows.Data Warning: 58 : BindingExpression (hash=14421545): Resolving source
System.Windows.Data Warning: 61 : BindingExpression (hash=14421545): Found data context element: TextBox (hash=35567111) (OK)
System.Windows.Data Warning: 62 : BindingExpression (hash=14421545): DataContext is null
System.Windows.Data Warning: 56 : BindingExpression (hash=14421545): Resolve source deferred
'WPF_Databind.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\PresentationFramework.Aero\3.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll'
System.Windows.Data Warning: 58 : BindingExpression (hash=14421545): Resolving source
System.Windows.Data Warning: 61 : BindingExpression (hash=14421545): Found data context element: TextBox (hash=35567111) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=14421545): Activate with root item Customer (hash=1669504)
System.Windows.Data Warning: 98 : BindingExpression (hash=14421545): At level 0 - for Customer.CustomerName found accessor <null>
System.Windows.Data Error: 35 : BindingExpression path error: 'CustomerName' property not found on 'object' ''Customer' (HashCode=1669504)'. BindingExpression:Path=CustomerName; DataItem='Customer' (HashCode=1669504); target element is 'TextBox' (Name='textBox1'); target property is 'Text' (type 'String')
System.Windows.Data Warning: 71 : BindingExpression (hash=14421545): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 77 : BindingExpression (hash=14421545): TransferValue - using fallback/default value ''
System.Windows.Data Warning: 78 : BindingExpression (hash=14421545): TransferValue - using final value ''

Available values for TraceLevel property are None,Low, Medium and High which result in different tracing details, and of course, don’t forget to remove it before releasing the application.

Note for Expression Blend users
It looks like that both Expression Blend 2.0 and 2.5 don’t recognize TraceLevel property so you won’t be able to see the artboard in presence of this attibute, but this will certainly be fixed in future releases.

posted on giovedì 3 aprile 2008 8.58 Print
Comments
Gravatar
# re: Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5
Bart
30/06/2008 9.37
  
Is there any way to throw an exception when a binding failure occurs ?
Gravatar
# re: Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5
adfdssd
25/11/2008 10.39
  
仓库除湿机
隧道风机
Gravatar
# re: Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5
flash games
14/04/2009 10.12
  
Well. Is there any way to throw an exception when a binding failure occurs
Gravatar
# re: Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5
Jim Jones
29/09/2009 16.45
  
some sort of exceptions would be helpful
Gravatar
# re: Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5
sadf
18/12/2009 8.31
  
Timberland Boots
Welcome to select the Cheap Timberland Boots
Timberland Work Boots
Timberland Boots Sale
Buy Timberland Boots
Timberland Classic
Timberland Mens 6 Inch
Timberland Mens Chukka
Timberland Mens Custom
TTimberland Mens Roll-Top
Timberland Womens 6 Inch
Timberland Womens High-Top
Timberland Womens Roll-Top
kids timberland boots
Mens Timberland Boots
Womens Timberland Boots
Cheap Kids Timberland Boots
Cheap Mens Timberland Boots
Cheap Womens Timberland Boots
cheap timberland classic boots
kids waterproof boots
classic timberland boots
Mens 6 Inch waterproof boots
Mens Chukka boots
Mens Custom boots
Mens Roll-Top boots
Womens 6 Inch boots
Womens High-Top boots
Womens Roll-Top boots
Gravatar
# re: Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5
replica watches
21/12/2009 13.16
  
Replica watches, like HUBLOT replica watches around on-line and in the super stores out there. These are the Swiss imitation watches that look exactly like their original counterparts. If you want to enjoy the services of these HUBLOT fake watches for exemple, you need to go for the best among them.
Best Replica watches come in different brands such as the Rolex replica watches, Breitling Replica Watches, Omega Replica watches, Hublot replica watches HUBLOT BIG BANG and others. These are all wonderful brands you can always go for
Best Replica watches like the brands already mentioned have great quality. They are made of high materials that put theme the same rank with the original Swiss watches.
Here the features to look out for in Best Replica watches.
Holiday period is indeed a time to spend money. You may like to cut your coat according to your size. Hence, making the choice to go for the Best Replica watches is the best decision you can make. This is because; Best Replica watches are indeed very cheap. With $88, such as China replica watches online stores: http://www.watch-jewelry-stores.com you can get a good brand of best Swiss Replica watch.
Another important point to remember is that Best Replica watches. have got the fake types available. You must have to take note of this fact. Hence as you go on line to purchase, watch out for the fake ones. One way to know them is by the inferior nature they are built and the very low prices like $70 or low they are being offered.
Gravatar
# ed hardy
ed hardy
29/12/2009 6.15
  
Looks very interesting. Thanks for sharing............ Gucci Shoes and cheap Gucci Shoes great post! ^ ^
Gravatar
# re: Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5
edhardy
02/02/2010 8.23
  
Life is like a box of chocolate. You never know Ed hardy what you're gonna to get.I love you not because of Ed hardy clothing who you are, but because of Ed hardy t-shirts who I am when I am Ed hardy bags with you.No Ed hardy tanks is worth your tears, and the one Ed hardy hoodies who is, argyleEd hardy boots won't make you cry.The worst way to miss Ed hardy discout shopsomeone is to be sitting right beside them knowing you Ed hardy shoes can't have them.Never frown, even Ed hardy shop when my fashion blogyou are sad, because you Pop clothing never know Pop fashion products who is falling in Ed hardy worldlove with your Popular designer space.
We are the best Ed hardy online,and our Ed hardy clothing also sale Ed hardy t-shirts,you can choose the newest Ed hardy bags,Ed hardy tanks , and Ed hardy hoodies, and other designer Ed hardy cheap shop ,our Ed hardy shoesare the best quality, also you can visit the Ed hardy shop.

Sex store
Sex Drugs
impotence drugs
impotence treatment
VIAGRA


Gravatar
# re: Find databinding mistakes using PresentationTracesources.TraceLevel in Fx 3.5
Abercrombie & Fitch
08/02/2010 8.29
  
Thanks for dropping by Links of London and

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 6 and 6 and type the answer here:
News

This is my personal blog.
These postings are provided "AS IS" with no warranties, and confer no rights.

Disciple