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)'
相关推荐
攻城狮CSU1 小时前
WPF 疑点汇总2.HorizontalAlignment和 HorizontalContentAlignment
wpf
c#上位机3 小时前
wpf之命令
c#·wpf
没有bug.的程序员3 小时前
分布式链路追踪:微服务可观测性的核心支柱
java·分布式·微服务·架构·wpf
Aevget21 小时前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(一)
c#·wpf·界面控件·devexpress·ui开发
The Sheep 20233 天前
WPF自定义路由事件
大数据·hadoop·wpf
阳光雨滴3 天前
使用wpf用户控件编程落石效果动画
c++·wpf
wuty0073 天前
WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离
wpf·sendmessage·changewindowmessagefilterex·uip·消息筛选器的用户界面特权隔离·window message
攻城狮CSU3 天前
WPF中核心接口 INotifyPropertyChanged
wpf
c#上位机3 天前
wpf之Interaction.Triggers
c#·wpf
是木子啦3 天前
wpf passwordbox控件 光标移到最后
c#·wpf