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)'
相关推荐
小白21 小时前
WPF自定义Dialog模板,内容用不同的Page填充
wpf
Crazy Struggle1 天前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
James.TCG2 天前
WPF动画
wpf
He BianGu2 天前
笔记:简要介绍WPF中FormattedText是什么,主要有什么功能
笔记·c#·wpf
脚步的影子2 天前
.NET 6.0 + WPF 使用 Prism 框架实现导航
.net·wpf
jyl_sh3 天前
Ribbon (WPF)
ribbon·wpf·client·桌面程序开发·c/s客户端
wo63704313 天前
[Visual Stuidio 2022使用技巧]2.配置及常用快捷键
c#·.net·wpf
小黄人软件3 天前
wpf 字符串 与 变量名或函数名 相互转化:反射
wpf
界面开发小八哥3 天前
DevExpress WPF中文教程:如何解决排序、过滤遇到的常见问题?(二)
.net·wpf·界面控件·devexpress·ui开发
Vae_Mars3 天前
WPF中图片的宫格显示
wpf