WPF如果未定义绑定的属性,程序如何处理

问题 :wpf中,<Button IsEnabled="{Binding IsValid1}"></Button>,如果没定义绑定的属性IsValid1,可以正常用吗

解答 :在 WPF 中,如果没有定义绑定的属性 IsValid1,会导致绑定失败,从而使 Button 的 IsEnabled 属性无法正常绑定。在这种情况下,Button 的 IsEnabled 属性将保持默认值,即为 true。

另外,错误信息会在vs运行时,在输入中体现:

C# 复制代码
System.Windows.Data Error: 12 : TargetNullValue '属性不存在' (type 'String') cannot be converted for use in 'IsEnabled' (type 'Boolean'). BindingExpression:Path=IsValid1; DataItem=null; target element is 'Button' (Name=''); target property is 'IsEnabled' (type 'Boolean') FormatException:'System.FormatException: 属性不存在 不是 Boolean 的有效值。 ---> System.FormatException: 该字符串未被识别为有效的布尔值。
   在 System.Boolean.Parse(String value)
   在 System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   在 System.Windows.Data.BindingExpressionBase.ConvertValue(Object value, DependencyProperty dp, Exception& e)'
相关推荐
国服第二切图仔1 小时前
鸿蒙应用开发之实现键值型数据库跨设备数据同步
数据库·wpf·harmonyos
玖笙&10 小时前
✨WPF编程进阶【7.1】动画基础
c++·c#·wpf·visual studio
专注VB编程开发20年10 小时前
探讨vs2022在net6框架wpf界面下使用winform控件
framework·.net·wpf·winform·cefsharp·miniblink·geckofx45
刘一说11 小时前
Spring Boot 中的定时任务:从基础调度到高可用实践
spring boot·后端·wpf
FuckPatience17 小时前
WPF 获取鼠标相对于控件的坐标信息,控制控件锚点放缩
wpf
兰雪簪轩1 天前
仓颉Actor模型:分布式并发编程的优雅之道
分布式·wpf
Crazy Struggle1 天前
WPF 如何支撑一个灵活的流程图编辑器?
.net·wpf·流程图
西岭千秋雪_3 天前
Zookeeper实现分布式锁
java·分布式·后端·zookeeper·wpf
beyond谚语3 天前
第三章 布局
wpf
Aevget3 天前
界面控件DevExpress WPF v25.1新版亮点:数据管理功能全新升级
.net·wpf·界面控件·devexpress·ui开发