获得许多具有约束力的“信息"在 WPF 输出窗口中 [英] Getting many Binding "Information" in WPF output window

查看:22
本文介绍了获得许多具有约束力的“信息"在 WPF 输出窗口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动我的应用程序时,我得到了很多这样的信息:

When I start my app I get a lot of information like this:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and 
no valid fallback value exists; using default instead. 
BindingExpression:Path=Period; DataItem=null; target element is 'TextBlock' (Name=''); 
target property is 'Text' (type 'String')

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and 
no valid fallback value exists; using default instead. 
BindingExpression:Path=DocumentId; DataItem=null; target element is 'TextBlock' (Name=''); 
target property is 'Text' (type 'String')

我应该关心那个输出吗?

Should I care for that output?

推荐答案

是的,你应该这样做.将绑定反馈(信息、错误等)就像编译器警告和错误一样处理是最安全的.绑定问题往往会减慢应用程序的执行速度,因为它会强制重新评估绑定,而且根据我的经验,有时会将它们中断到需要在代码中重新创建绑定的地方.

Yes, you should. It is safest to treat binding feedback (information, errors, etc) just like compiler warning and errors. Binding issues tend to slow the execution of the application down since it forces bindings to be re-evaluated, and in my experience, sometimes breaks them to where you need to recreate the binding in code.

如果可以,最简单的解决方法是将绑定上的 FallbackValue 设置为可被视为默认值的值.例如,如果您有一个具有Count"属性的类,但该类在创建视图时的某个时间点可能为空,则您的绑定可能看起来像 <TextBlock Text={Binding Count,FallbackValue=0}/> 这将在文本块中显示0",或者将后备值传递给转换器(如果有的话).

The simplest workaround, if you can, is to set the FallbackValue on your binding to a value which can be treated as default. For example, if you have a class which has a "Count" property, but said class can be null at some point in time when your view is created, your binding might look like <TextBlock Text={Binding Count, FallbackValue=0} /> which would display a "0" in the text block, or pass the fallback value to the converter if there is one in use.

这篇关于获得许多具有约束力的“信息"在 WPF 输出窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆