I would like a simple way to ensure that all bindings I've declared in my xaml files go to real properties. Even better, I'd like to instantiate my wpf window in a unit test and call a method to ensure that the bindings are correct.
我希望有一种简单的方法来确保我在xaml文件中声明的所有绑定都转到真正的属性。更好的是,我想在单元测试中实例化我的wpf窗口,并调用一个方法来确保绑定是正确的。
Unfortunately, wpf doesn't even throw an exception if I have something wrong. That leaves me the burden to "discover" problems during a QA phase.
不幸的是,如果我出了问题,wpf甚至不会抛出异常。这让我在QA阶段“发现”问题。
Does anyone know of a way I can better validate my bindings?
有人知道我可以更好地验证我的绑定吗?
2 个解决方案
#1
4
A suboptimal way would be to search through the visual tree for all dependency properties, and then check:
一种次优方法是在视觉树中搜索所有依赖项属性,然后检查:
var bindingExpression = BindingOperations.GetBindingExpressionBase(dependencyObject, dependencyProperty);
if (bindingExpression != null)
{
var status = bindingExpression.Status;
}
If the status
is Unattached
then the expression hasn't resolved.
如果状态是Unattached的,那么表达式还没有解析。
Of course, you wouldn't want to do this in a production app, but it might make sense in a debug or integration test scenario.
当然,您不会希望在生产应用程序中这样做,但在调试或集成测试场景中,这可能是有意义的。
#2
1
Data binding errors show up in the Visual Studio Output window. For example, say I want to bind a TextBlock to the Title property of a Window but I mistype "Title" as "Ritle". I will see this in the output window:
数据绑定错误显示在Visual Studio输出窗口中。例如,假设我想将一个TextBlock绑定到一个窗口的Title属性,但是我将“Title”作为“Ritle”。我会在输出窗口中看到:
System.Windows.Data Error: 39 : BindingExpression path error: 'Ritle' property not found on 'object' ''MessageWindow' (Name='Window')'. BindingExpression:Path=Ritle; DataItem='MessageWindow' (Name='Window'); target element is 'TextBlock' (Name='WindowTitle'); target property is 'Text' (type 'String')
System.Windows。数据错误:39:BindingExpression path错误:'Ritle'属性没有在'object' " MessageWindow' (Name='Window')上找到。BindingExpression:路径= Ritle;DataItem = ' MessageWindow '(Name =“窗口”);目标元素为'TextBlock' (Name='WindowTitle');目标属性是'Text'(输入'String')
You can get more control over how these messages are reported by using Trace Sources. This article by Bea Stollnitz describes this in greater detail.
您可以通过使用跟踪源对这些消息的报告方式进行更多的控制。Bea Stollnitz的这篇文章更详细地描述了这一点。
#1
4
A suboptimal way would be to search through the visual tree for all dependency properties, and then check:
一种次优方法是在视觉树中搜索所有依赖项属性,然后检查:
var bindingExpression = BindingOperations.GetBindingExpressionBase(dependencyObject, dependencyProperty);
if (bindingExpression != null)
{
var status = bindingExpression.Status;
}
If the status
is Unattached
then the expression hasn't resolved.
如果状态是Unattached的,那么表达式还没有解析。
Of course, you wouldn't want to do this in a production app, but it might make sense in a debug or integration test scenario.
当然,您不会希望在生产应用程序中这样做,但在调试或集成测试场景中,这可能是有意义的。
#2
1
Data binding errors show up in the Visual Studio Output window. For example, say I want to bind a TextBlock to the Title property of a Window but I mistype "Title" as "Ritle". I will see this in the output window:
数据绑定错误显示在Visual Studio输出窗口中。例如,假设我想将一个TextBlock绑定到一个窗口的Title属性,但是我将“Title”作为“Ritle”。我会在输出窗口中看到:
System.Windows.Data Error: 39 : BindingExpression path error: 'Ritle' property not found on 'object' ''MessageWindow' (Name='Window')'. BindingExpression:Path=Ritle; DataItem='MessageWindow' (Name='Window'); target element is 'TextBlock' (Name='WindowTitle'); target property is 'Text' (type 'String')
System.Windows。数据错误:39:BindingExpression path错误:'Ritle'属性没有在'object' " MessageWindow' (Name='Window')上找到。BindingExpression:路径= Ritle;DataItem = ' MessageWindow '(Name =“窗口”);目标元素为'TextBlock' (Name='WindowTitle');目标属性是'Text'(输入'String')
You can get more control over how these messages are reported by using Trace Sources. This article by Bea Stollnitz describes this in greater detail.
您可以通过使用跟踪源对这些消息的报告方式进行更多的控制。Bea Stollnitz的这篇文章更详细地描述了这一点。