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 到 Avalonia 的迁移系列实战篇6:Trigger、MultiTrigger、DataTrigger 的迁移
wpf·avalonia
招风的黑耳1 天前
Java生态圈核心组件深度解析:Spring技术栈与分布式系统实战
java·spring·wpf
lfw20191 天前
WPF 数据绑定模式详解(TwoWay、OneWay、OneTime、OneWayToSource、Default)
wpf
Magnum Lehar1 天前
3d wpf游戏引擎的导入文件功能c++的.h实现
3d·游戏引擎·wpf
FuckPatience2 天前
WPF Telerik.Windows.Controls.Data.PropertyGrid 自定义属性编辑器
wpf
almighty272 天前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
军训猫猫头2 天前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
我要打打代码2 天前
在WPF项目中使用阿里图标库iconfont
wpf
拾忆,想起3 天前
Redisson 分布式锁的实现原理
java·开发语言·分布式·后端·性能优化·wpf
weixin_464078073 天前
wpf依赖注入驱动的 MVVM实现(含免费源代码demo)
wpf