Search code examples
wpftrace

Use of System.Diagnostics.PresentationTraceSources.SetTraceLevel() method


I read the documentation of SetTraceLevel() method and couldn't get anything out of that 2 line documentation.

Can any one throw some light on it.


Solution

  • I've only used it on bindings, not sure if there are/aren't any other ways to use it though...

    In XAML, add the namespace:

    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    

    Then, you can set the trace level in a binding:

    <TextBox Text="{Binding Path=TextField, UpdateSourceTrigger=PropertyChanged, diag:PresentationTraceSources.TraceLevel=High}" />
    

    FYI - Intellisense doesn't seem to assist with typing it in.

    If you wanted to do it in code and use the method you linked:

    TextBox tb = new TextBox();
    Binding b = new Binding();
    b.Path = new PropertyPath("TextField", new object[] { });
    b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    System.Diagnostics.PresentationTraceSources.SetTraceLevel(b, System.Diagnostics.PresentationTraceLevel.High);
    tb.SetBinding(TextBox.TextProperty, b);
    

    Either way, the trace shows up in VS 2015's output window (VM is the DataContext, 'This is a test.' is the default value of property TextField).

    System.Windows.Data Warning: 56 : Created BindingExpression (hash=25035015) for Binding (hash=19986012)
    System.Windows.Data Warning: 58 :   Path: 'TextField'
    System.Windows.Data Warning: 60 : BindingExpression (hash=25035015): Default mode resolved to TwoWay
    System.Windows.Data Warning: 62 : BindingExpression (hash=25035015): Attach to System.Windows.Controls.TextBox.Text (hash=23243381)
    System.Windows.Data Warning: 67 : BindingExpression (hash=25035015): Resolving source 
    System.Windows.Data Warning: 70 : BindingExpression (hash=25035015): Found data context element: TextBox (hash=23243381) (OK)
    System.Windows.Data Warning: 78 : BindingExpression (hash=25035015): Activate with root item VM (hash=48624771)
    System.Windows.Data Warning: 108 : BindingExpression (hash=25035015):   At level 0 - for VM.TextField found accessor RuntimePropertyInfo(TextField)
    System.Windows.Data Warning: 104 : BindingExpression (hash=25035015): Replace item at level 0 with VM (hash=48624771), using accessor RuntimePropertyInfo(TextField)
    System.Windows.Data Warning: 101 : BindingExpression (hash=25035015): GetValue at level 0 from VM (hash=48624771) using RuntimePropertyInfo(TextField): 'This is a test.'
    System.Windows.Data Warning: 80 : BindingExpression (hash=25035015): TransferValue - got raw value 'This is a test.'
    System.Windows.Data Warning: 89 : BindingExpression (hash=25035015): TransferValue - using final value 'This is a test.'
    

    Changing the text (added another '.') in the textbox causes this output:

    System.Windows.Data Warning: 90 : BindingExpression (hash=25035015): Update - got raw value 'This is a test..'
    System.Windows.Data Warning: 94 : BindingExpression (hash=25035015): Update - using final value 'This is a test..'
    System.Windows.Data Warning: 102 : BindingExpression (hash=25035015): SetValue at level 0 to VM (hash=48624771) using RuntimePropertyInfo(TextField): 'This is a test..'
    System.Windows.Data Warning: 95 : BindingExpression (hash=25035015): Got PropertyChanged event from VM (hash=48624771)
    System.Windows.Data Warning: 101 : BindingExpression (hash=25035015): GetValue at level 0 from VM (hash=48624771) using RuntimePropertyInfo(TextField): 'This is a test..'
    System.Windows.Data Warning: 80 : BindingExpression (hash=25035015): TransferValue - got raw value 'This is a test..'
    System.Windows.Data Warning: 89 : BindingExpression (hash=25035015): TransferValue - using final value 'This is a test..'
    

    EDIT:

    https://msdn.microsoft.com/en-us/library/system.diagnostics.presentationtracesources(v=vs.100).aspx

    Looks like it should work on a few other things than just Bindings.