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)'
相关推荐
cjp5606 小时前
023.WPF combox控件数据绑定
wpf
Scout-leaf7 小时前
WPF新手村教程(七)—— 终章(MVVM架构初见杀)
c#·wpf
极客智造8 小时前
WPF DataGrid 多选绑定 + 强类型命令回调 通用解决方案
wpf
ZoeJoy88 小时前
机器视觉C# 调用相机:从 USB 摄像头到海康工业相机(WinForms & WPF)
数码相机·c#·wpf
ALex_zry1 天前
C++高性能日志与监控系统设计
c++·unity·wpf
zhojiew1 天前
使用flink agent框架实现流式情感分析的示例
大数据·flink·wpf
海盗12341 天前
ScottPlot在WPF的基本使用和中文乱码问题
c#·.net·wpf
俄城杜小帅1 天前
C++线程异步和wpf中比较
java·c++·wpf
ZoeJoy81 天前
WPF 从入门到实践:基础、ModernUI 与 MVVM 完全指南
c#·wpf
△曉風殘月〆2 天前
WPF Prism中的MVVM实现
wpf·mvvm