如何解决此绑定错误 [英] How to resolve this binding error

查看:82
本文介绍了如何解决此绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var oBackgroundGrid = new Grid
           {
               Opacity = .5 ,
               DataContext = PortSettings
           };

           oBackgroundGrid.SetBinding ( Panel.BackgroundProperty ,

           new Binding
           {
               Source = PortSettings,
               Path = new PropertyPath ( "PortSettings.CActual" ) ,
               Converter = new ColorTemplateSelector ( ) ,
               UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged ,
               Mode = BindingMode.TwoWay
           } );





i我在代码后面创建一个网格,但它显示绑定错误为





i am creating a grid in code behind but it showing binding error as

System.Windows.Data Error: 40 : BindingExpression path error: 'PortSettings' property not found on 'object' ''CheckboxSetting' (HashCode=63345626)'. BindingExpression:Path=PortSettings.CActual; DataItem='CheckboxSetting' (HashCode=63345626); target element is 'Grid' (Name=''); target property is 'Background' (type 'Brush')





我尝试过:



i尝试一切,不知道是什么错误



What I have tried:

i try everything, don't know what is the error

推荐答案

花了我一秒钟,但我想我知道这里发生了什么。应将 Source 设置为包含您要访问的属性的对象(或父级),而 Path 是从源获取该属性的路径。尝试

Took me a second but I think I know what's going on here. The Source should be set to the object (or parent) which contains the property you wish to access while the Path is the path to that property from the source. Try
new Binding
{
Source = this,
Path = new PropertyPath ( "PortSettings" ) ,
Converter = new ColorTemplateSelector ( ) ,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged ,
Mode = BindingMode.TwoWay
}





编辑:注意:如果你想访问的属性 PortSettings CActual 您需要强制转换属性或将属性类型设置为正确的类型而不是对象。然后只需将路径更改为新的PropertyPath(PortSettings.CActual)



Note: If you want to access a property of PortSettings like CActual you need to cast the property or set the type of property to the proper type instead of object. Then just change the Path to new PropertyPath("PortSettings.CActual").


这篇关于如何解决此绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆