WPF DataTemplate does not throw an exception

When developing a new WPF application, I noticed exceptions that were not selected during data binding for controls using DataTemplates. For testing, I wrote the following simple user control with minimal logic. I am using .NET 3.5 SP1, VS2008 SP1 running on WIN7.

When the DataContext is set, TestMethod is called and an exception is thrown in the code, but the application is not interrupted.

Will anyone think about what happens when creating a DataTemplate? In particular, I am interested in where the exception occurs.

The following is the XAML code:

<UserControl x:Class="WPF2008.DataTemplateQuestion"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
    <UserControl.Resources>
        <DataTemplate x:Key="TESTKey">
            <Border BorderBrush="Black" BorderThickness="10">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBox Grid.Row="0" Text="{Binding Path=Txt}" />
                    <TextBox Grid.Row="1" Text="{Binding Path=Lbl}" />
                    <TextBox Grid.Row="2" Text="Am I disabled?" />
                    <TextBox Grid.Row="3" Text="I am disabled." IsEnabled="False" />
                </Grid>
            </Border>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <Label x:Name="lblTest" Content="{Binding Path=TestMethod}" ContentTemplate="{StaticResource TESTKey}" />
    </Grid>
</UserControl>

Below codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF2008
{
    public partial class DataTemplateQuestion : UserControl
    {
        public DataTemplateQuestion()
        {
            try
            {
                InitializeComponent();
                lblTest.DataContext = new TestClass();
            }
            catch (Exception e)
            {
                // ADDING A BREAKPOINT HERE DOESN'T CATCH THE EXCEPTION
                throw e; 
            }
        }
    }

    public class TestClass
    {
        public TestValue TestMethod
        {
            get
            {
                // BREAKPOINT WILL BE HIT WHEN DEBUGGING BUT THE EXCEPTION DISAPPEARS
                throw new Exception("WATCH ME DISAPPEAR");
                return new TestValue { Lbl = "Label", Txt = "Text" };
            }
        }

        public class TestValue
        {
            public string Lbl { get; set; }
            public string Txt { get; set; }
        }
    }
}
+3
source share
2 answers

, , , DataBindingSource TraceSource, "System.Windows.Data".

, TraceListner, Listeners TraceSourceSource. , App.config.

:

System.Diagnostics.PresentationTraceSources.DataBindingSource.Listeners.Add(
  new MyTraceListener());

. TraceSource TraceListener.

+1
+1

Source: https://habr.com/ru/post/1734897/


All Articles