0

I'm trying to debugging a binding issue in a command. Enabled all the tracing in the options menu, added listeners on xaml and on app (like suggested here https://spin.atomicobject.com/2013/12/11/wpf-data-binding-debug/) but still there's no way to get error in the output window.

This is the XAML that is causing me problems. Added non existing binding but not able to get tracing

<Page
x:Class="EyesGuard.Views.Pages.CustomPause"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:EyesGuard.Views.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lang="clr-namespace:EyesGuard.Localization"
xmlns:local="clr-namespace:EyesGuard.Views.Pages"
xmlns:l="clr-namespace:EyesGuard.Localization"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="{lang:LocalizedString 'EyesGuard.CustomPause.PageTitle'}"
d:DesignHeight="450"
x:Name="this"
FlowDirection="{lang:LocalizedFlowDirection}"
d:DesignWidth="550"
Background="#FF006A9F"
FontFamily="{StaticResource HastiUI.Fonts.IRANSans}"
Loaded="Page_Loaded"
mc:Ignorable="d">
<Grid>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock
            FontSize="20"
            Foreground="White"
            Text="{lang:LocalizedString 'EyesGuard.CustomPause.Message'}" />
        <StackPanel
            Grid.Row="2"
            Grid.Column="2"
            Margin="5"
            Orientation="Horizontal">
            <c:NumOnlyTextbox
                MinWidth="50"
                Margin="0"
                MaxLength="2"
                Style="{DynamicResource WhiteTextBox}"
                Text="{Binding Path=HoursCustomPause}"
                TextAlignment="Center" />
            <TextBlock
                Margin="5,0,0,0"
                VerticalAlignment="Center"
                Foreground="White"
                Text="{lang:LocalizedString 'EyesGuard.Settings.TimeSeparators.Hour'}" />
            <c:NumOnlyTextbox
                MinWidth="50"
                Margin="5,0,0,0"
                MaxLength="2"
                Style="{DynamicResource WhiteTextBox}"
                Text="{Binding Path=MinutesCustomPause}"
                TextAlignment="Center" />
            <TextBlock
                Margin="5,0,0,0"
                VerticalAlignment="Center"
                Foreground="White"
                Text="{lang:LocalizedString 'EyesGuard.Settings.TimeSeparators.Minutes'}" />
            <c:NumOnlyTextbox
                MinWidth="50"
                Margin="5,0,0,0"
                MaxLength="2"
                Style="{DynamicResource WhiteTextBox}"
                Text="{Binding Path=SecondssCustomPause}"
                TextAlignment="Center" />
            <TextBlock
                Margin="5,0,0,0"
                VerticalAlignment="Center"
                Foreground="White"
                Text="{lang:LocalizedString 'EyesGuard.Settings.TimeSeparators.Second'}" />

        </StackPanel>
        <Button
            Width="120"
            Margin="0,10,0,0"
            Padding="10,4"
            Command="{Binding Path=PauseProtection, diag:PresentationTraceSources.TraceLevel=High}"
            Content="{lang:LocalizedString     'EyesGuard.CustomPause.ButtonText'}"
            Style="{DynamicResource ButtonTemplate.Green}" />
    </StackPanel>
</Grid>

gianpaolo
  • 753
  • 1
  • 14
  • 26
  • 1
    Please add a [reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to your question. If you're seeing nothing in the output window in VS, the binding is never trying to resolve the source property. That sounds to me like the XAML with that Button in it is never loaded. The cause of the problem is not in the tiny snippet of XAML you shared. If you provide a reproducible example, the cause of the problem will almost certainly be obvious. **Pro-tip**: `PresentationTraceSources` is already defined. You're free to omit the `diag:` prefix. – 15ee8f99-57ff-4f92-890c-b56153 Aug 27 '19 at 13:25
  • @EdPlunkett Hi Ed, thanks for your reply. The issue is that XAML was pure code behind without DataContext. Now that I've added a ViewModel and a DataContext I can see the errors. The code is not mine, so I'm trying to debugging the problems I find. I don't know if I have to delete this question. Thanks for the Pro-tip! – gianpaolo Aug 27 '19 at 13:56
  • If the XAML has a binding, the binding will try to resolve its source, regardless of whether there's a DataContext or not. Bindings don't need a datacontext; there are other ways to set the source (`RelativeSource`, `ElementName`). So what you're saying here is not generally true. It will be true if the Button is being created by an implicit DataTemplate that's only applied when there's a DataContext. Unless you plan to add a [reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to the question, I would personally delete it. – 15ee8f99-57ff-4f92-890c-b56153 Aug 27 '19 at 14:07
  • @EdPlunkett So now I'm really curious: if I set the DataContext, I can see all the listeners get triggered; if not, no trace at all. I've added the step to reproduce the issue (it's a project I've found on github) – gianpaolo Aug 27 '19 at 14:18
  • I linked the MVCE article twice. Please read it. People are busy, you can't ask them to go off site, download a project from github, edit it, and run it. Just provide a minimal, reproducible example in the text of your question. – 15ee8f99-57ff-4f92-890c-b56153 Aug 27 '19 at 14:24
  • @EdPlunkett Ed, I'm not asking anything anyone, I think this could be useful. It's not a C function that can be copy/pasted, it's a wpf solution with some settings in it that is probably causing the problem. There's no need to downvote at every comment I add. – gianpaolo Aug 27 '19 at 14:31
  • Also, the XAML is loaded, otherwise I wouldn't ask help. – gianpaolo Aug 27 '19 at 14:41
  • 1
    "Debug tracing is only available when a WPF application is running in full trust mode. In order to enable tracing, you first must set a registry key, then you must configure trace sources. To create the registry key, set a "ManagedTracing" reg_dword value to 1 under "HKeyCurrentUser\Software\Microsoft\Tracing\WPF". To configure trace sources, create an application config file. This file has a .config extension, for example, XamlPad.exe.config." – BionicCode Aug 27 '19 at 14:55
  • You haven't provided anything that reproduces what you claim to be seeing: Without DataContext, default minimal one-line binding errors will not appear in Output. However, **in the example you provided**, as elsewhere, `PresentationTraceSources.TraceLevel=High` (with or without `diag:`) produces the expected diagnostics regardless of whether DataContext is null or not. This would absolutely be useful **if you provide a reproducible example**. We don't ask for reproducible examples just to be annoying. – 15ee8f99-57ff-4f92-890c-b56153 Aug 27 '19 at 14:56
  • @BionicCode Thank you. But why the trace appears if I add a DataContext in the code behind? That should not happen if it was a missing registry key, what do u think? I'm puzzled. Never seen this happening on WPF – gianpaolo Aug 27 '19 at 14:59
  • @gianpaolo Tools | Options | Debugging / Output Window / WPF Trace Settings – 15ee8f99-57ff-4f92-890c-b56153 Aug 27 '19 at 15:06
  • Do you see the info of the particular binding that you attached the property to? I think you are just seeing the default output of the debugger. You can adjust those trace level settings via Debug/Options/Debugging/Output Window/WPF Trace Settings. – BionicCode Aug 27 '19 at 15:07
  • @EdPlunkett Again. The reproducible example IS the solution. It's fine if you don't have time to look at it, I perfectly understand it, but don't downvote for that. The XAML itself does not say anything and the code behind is just InitializeComponent(), very simple. I'm not the top programmer but I know what I'm doing: DataContext in the code behind set -> trace logged and even a message box is displayed. DataContext not set, command binding is not traced. Other people have seen it: https://stackoverflow.com/questions/39663182/wpf-binding-errors-not-showing-up-anywhere-no-matter-what-i-do – gianpaolo Aug 27 '19 at 15:07
  • Everthing else is just wild guessing. As Ed Plunkett said before you should create a minimum running code exa,mple that reproduces your problem. Otherwise I don't think anybody can help you. – BionicCode Aug 27 '19 at 15:08
  • @BionicCode Yes, I had previously enabled all the wpf trace settings. – gianpaolo Aug 27 '19 at 15:10
  • @BionicCode All the WPF apps I have work ok, it's just this one that is behaving in this way. Taht's why I pointed to the solution. I can't reproduce this behaviour in other scenario. It's definitely very weird. I've added the XAML but I can't see anything strange in it, it's so simple. Thanks a lot for your help! – gianpaolo Aug 27 '19 at 15:13
  • See? The output you are able to see is not coming from the `PresentationTraceSources`. It's the default debugger output. Doesn't this output is enough to tell you that the command binding is not working? Set the level to at least 'Information'. Otherwise check if you meet all requirements to enable `PresentationTraceSources` support. Why the `DataContext` should influence the debugger logging behavior is not reasonable to me. The XAML parser will always try to resolve any binding and therefor produce log output. – BionicCode Aug 27 '19 at 15:32
  • Alternatively use a value converter with the binding to check if it is active and what values are actually send. – BionicCode Aug 27 '19 at 15:32
  • Your code produces output on my machine, no matter the DataContext. But this is what I expected. – BionicCode Aug 27 '19 at 15:41
  • Again, check your configurations in order to use `PresentationTraceSources.TraceLevel`. – BionicCode Aug 27 '19 at 15:45
  • 1
    @BionicCode I agree with you about DataContext, but after playing with sample app and all sort of tracing, I've tried with the DataContext just as a last resort. I wil try with a value converter. And yes, that XAML works if I copy paste it in another app. :( Thanks for oyour time! – gianpaolo Aug 27 '19 at 15:59

0 Answers0