- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在设计新的 WPF 应用程序时,我注意到在使用 DataTemplates 对控件进行数据绑定(bind)期间不会抛出异常。为了测试,我用尽可能少的逻辑编写了以下简单的用户控件。我正在使用在 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; }
}
}
}
最佳答案
数据绑定(bind)抛出的异常被转换为跟踪,被传送到 DataBindingSource
TraceSource
,其源名称为“System.Windows.Data”。
您可以通过创建在跟踪上抛出异常的 TraceListner
的子类将它们转换为异常,并将其添加到 TraceSourceSource 的
。这可以在代码中或在您的 Listeners
集合中App.config
文件中完成。
以下是您在代码中的做法:
System.Diagnostics.PresentationTraceSources.DataBindingSource.Listeners.Add(
new MyTraceListener());
有关详细信息,请参阅 TraceSource 和 TraceListener 文档和示例。
关于WPF DataTemplate 不抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2357046/
我正在使用 DataTemplate 将 View 应用于 ViewModel。我有一种情况,当 View 的一个实例(DataTemplate)中发生某件事时,我需要在所有其他实例中采取行动。 我已
如何从 DataTemplate 派生新类,然后用它代替 DataTemplate? 例如,在 C# 中: public class DerivedDataTemplate : DataTemplat
我有以下xaml; 当我使用 Node 模板显示 No
我想在 dataTemplate 中使用 dataTemplale。我想像这样在列表框中显示数据: 这就是我得到的。它不起作用。 cl
大家好,我正在尝试更改 dataGrid 中列的模板,但我找不到在 XAML 中执行此操作的方法。我正在尝试通过这种方式来做
wpf 应用程序中的默认 DataTemplate 显示 .ToString() 方法的结果。我正在开发一个默认 DataTemplate 不显示任何内容的应用程序。 我试过:
我发现自己有点束手无策……有束缚……呵呵……(跛脚) 无论如何...我需要引用主 ViewModel 的属性,但来自 DataTemplate ,它本身在另一个 DataTemplate 中... 看
我已经为我喜欢的项目中的一些数据类型创建了几个数据模板。这些数据模板真的很酷,因为它们像魔术一样工作,无论何时何地出现在 UI 中,都神奇地改变了数据类型实例的外观。现在我希望能够在一个特定的列表框中
这个问题在这里已经有了答案: 11年前关闭。 Possible Duplicate: Datatemplate inheritance 我有几个不是子类的数据类型,它们也不共享一个接口(interfa
我有一个用代码隐藏在 XAML 中定义的 ResouceDictionary。我需要使用鼠标事件和数据绑定(bind)定义一些特定于 View 的行为,为此我需要访问在 DataTemplate 中定
我有一个用代码隐藏在 XAML 中定义的 ResouceDictionary。我需要使用鼠标事件和数据绑定(bind)定义一些特定于 View 的行为,为此我需要访问在 DataTemplate 中定
我创建了一个 DataTemplateSelector,它使用一组已知接口(interface)进行初始化。如果传递到选择器的项目实现了这些接口(interface)之一,则返回关联的数据模板。 首先
是否可以将数据模板用于没有 ListBox 或其他项目控件的单个项目? 我有一个数据模板,我想在 xaml 中实例化,而不是在列表中,只是在边框内,并设置其数据上下文。 类似(伪):
在我的 MainWindow.xaml 中,我对 ResourceDictionary 有以下引用: 在 Main
我在数据模板中有 WPF ComboBox(列表框中有很多组合框),我想处理输入按钮。如果它是例如,那将很容易。一个按钮 - 我会使用命令 + 相对绑定(bind)路径等。不幸的是,我不知道如何使用命
我想对 ItemsCollection 进行数据绑定(bind),但不是渲染集合项,而是渲染通过集合项上的属性到达的子对象。 更具体地说:这将是游戏的 2D map 查看器(尽管在当前状态下它还不是
我使用 MasterDetailsView控制。它有 MasterHeaderTemplate属性(property)。我想添加一个 TextBox以便执行元素搜索。我不明白该怎么做。因为DataTe
我为自己设计的一个非常基本的 WPF 练习遇到了一个奇怪的问题,即从 ViewModel 动态填充菜单。给定以下主窗口标记:
在设计新的 WPF 应用程序时,我注意到在使用 DataTemplates 对控件进行数据绑定(bind)期间不会抛出异常。为了测试,我用尽可能少的逻辑编写了以下简单的用户控件。我正在使用在 WIN7
我有一个关于 this one 的问题:我正在尝试将一个事件附加到我的 StackPanel,但在使用 XamlReader 时它似乎没有连接。我无法调用 ChildItem_Load 方法。有谁知道
我是一名优秀的程序员,十分优秀!