【问题标题】:WPF DataTemplate not throwing exceptionWPF DataTemplate 不抛出异常
【发布时间】:2010-03-01 15:56:12
【问题描述】:

在设计新的 WPF 应用程序时,我注意到在使用 DataTemplates 进行控件的数据绑定期间未引发异常。为了测试,我用尽可能少的逻辑编写了以下简单的用户控件。我正在使用在 WIN7 上运行的 .NET 3.5 SP1、VS2008 SP1。

设置 DataContext 后,调用 TestMethod 并在代码隐藏中引发异常,但应用程序不会中断。

有人介意解释创建 DataTemplate 时发生的情况吗?具体来说,我对异常的去向感兴趣。

以下是 XAML 代码:

<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>

下面是代码隐藏:

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; }
        }
    }
}

【问题讨论】:

    标签: wpf exception datatemplate


    【解决方案1】:

    数据绑定抛出的异常被转换成跟踪,被传递到DataBindingSourceTraceSource,其源名称为“System.Windows.Data”。

    您可以通过创建在跟踪上引发异常的TraceListner 的子类将这些转换为异常,并将其添加到TraceSourceSourceListeners 集合中。这可以在代码或App.config 文件中完成。

    以下是您在代码中的操作方式:

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

    有关详细信息,请参阅 TraceSource 和 TraceListener 文档和示例。

    【讨论】:

      【解决方案2】:

      异常被数据绑定吃掉,并导致数据绑定被禁用(我不是 100% 确定这里是这种情况,我懒得把它放到 VS 中)。

      另一种可能性是,如果您运行的是 64 位操作系统,您可能会点击 http://support.microsoft.com/kb/976038

      【讨论】:

      • 我更改了代码以测试数据绑定是否被禁用,但发现它只显示没有数据(参见新代码)。我正在运行 Win7 x64,因此安装了修补程序并在系统级别启用了它,但没有改变。还有其他想法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      • 2012-01-24
      • 2013-05-24
      • 2010-12-09
      相关资源
      最近更新 更多