WPF 错误 40 BindingExpression 路径错误:在“对象"上找不到属性 [英] WPF Error 40 BindingExpression path error: property not found on 'object'

查看:28
本文介绍了WPF 错误 40 BindingExpression 路径错误:在“对象"上找不到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这个绑定错误,我正把头撞在我的桌子上.>

无论如何,我有一个名为 IncrementingTextBox 的自定义控件.每当用户检查"其上方的 CheckBox 时,我都会尝试禁用它.

我在 CheckBox IsChecked 属性上有一个绑定,该属性工作正常并且在它应该触发时触发.它正确设置了 ConfigurationModel 上的 UseSensorLength 属性.

但是,IncrementingTextBox IsEnabled 属性上的绑定会导致 BindingExpression 路径错误,因此根本不会更新.

作为测试,我尝试在后面的代码中启用和禁用控件,它工作得很好,但我似乎无法让绑定在它上面工作.

这是我的 xaml 的一个片段:

<代码>...DataContext="{Binding RelativeSource={RelativeSource Self}}"......<CheckBox Content="使用传感器长度" Margin="30,6,0,0"IsChecked="{Binding ConfigurationModel.UseSensorLength, Mode=TwoWay}"/><local:IncrementingTextBox x:Name="video_length_textbox" Margin="0,0,0,5"IsTextEnabled="假"IsEnabled="{Binding ConfigurationModel.DontUseSensorLength}"ValueChanged="VideoEventValueChanged"/>

这是我的 ConfigurationModel 的一个片段:

public bool DontUseSensorLength{得到 { 返回 !UseSensorLength;}}public bool UseSensorLength{得到 { 返回 _useSensorLength;}放{_useSensorLength = 值;OnPropertyChanged("UseSensorLength");OnPropertyChanged("DontUseSensorLength");}}

这是我在运行应用程序时在输出窗口中收到的错误消息:

<块引用>

System.Windows.Data 错误:40:BindingExpression 路径错误:在对象"上找不到ConfigurationModel"属性''IncrementingTextBox' (Name='video_length_textbox')'.BindingExpression:Path=ConfigurationModel.DontUseSensorLength;DataItem='IncrementingTextBox' (Name='video_length_textbox');目标元素是 'IncrementingTextBox' (Name='video_length_textbox');目标属性是IsEnabled"(类型布尔")

请记住,UseSensorLength"属性绑定工作正常,但DontUseSensorLength"绑定会导致上述BindingExpression 路径错误".

解决方案

我最近写了一些其他 SO answer 关于如何阅读绑定错误,使它们更有意义.总而言之,在冒号和分号的错误消息中添加换行符,并从下往上阅读.

您的错误信息是:

  • System.Windows.Data 错误:40:
    • BindingExpression 路径错误:在 'object' ''IncrementingTextBox' (Name='video_length_textbox')' 上找不到 'ConfigurationModel' 属性.
    • BindingExpression:Path=ConfigurationModel.DontUseSensorLength;
  • DataItem='IncrementingTextBox' (Name='video_length_textbox');
  • 目标元素是 'IncrementingTextBox' (Name='video_length_textbox');
  • 目标属性是'IsEnabled'(类型'Boolean')

这可以从下往上读为:

  • 绑定失败是 IncrementingTextBox 类型元素的 IsEnabled 属性(名为 video_length_textbox).

  • 元素的DataItem(DataContext)是一个名为video_length_textbox的IncrementingTextBox类型的对象

  • 它试图找到的绑定表达式是 ConfigurationModel.DontUseSensorLength

  • 绑定的问题是在数据上下文对象 IncrementingTextBox

  • 上找不到 ConfigurationModel 属性

因此您的video_length_textbox"的 DataContext 设置为自身,并且您的 IncrementingTextBox 类没有名为 ConfigurationModel 的公共属性>

由于我没有看到您在 XAML 的任何位置为 IncrementingTextBox 设置了 DataContext,请查看您的 IncrementingTextBox 类的代码.最可能的情况是您在构造函数中将 DataContext 设置为自身

this.DataContext = this;

或 XAML

DataContext="{Binding RelativeSource={RelativeSource Self}}"

I'm banging my head on my desk with this binding error.. I have checked several of the postings for the BindingExpression path error and cannot see anything that works with my situation.

Anyway, I have a custom control called IncrementingTextBox. I am trying to disable it whenever the user 'checks' the CheckBox above it.

I have a binding on the CheckBox IsChecked property that is working fine and is firing when it is supposed to. It is correctly setting the UseSensorLength property on the ConfigurationModel.

However, the binding on the IncrementingTextBox IsEnabled property is causing a BindingExpression path error and so doesn't update at all.

As a test, I tried in the code behind to enable and disable the control and it works just fine, but I can't seem to get the Binding to work on it.

Here is a snippet from my xaml:

...

DataContext="{Binding RelativeSource={RelativeSource Self}}"

...
...

<CheckBox Content="Use Sensor Length" Margin="30,6,0,0" 
          IsChecked="{Binding ConfigurationModel.UseSensorLength, Mode=TwoWay}"/>

<local:IncrementingTextBox x:Name="video_length_textbox" Margin="0,0,0,5" 
                           IsTextEnabled="False" 
                           IsEnabled="{Binding ConfigurationModel.DontUseSensorLength}" 
                           ValueChanged="VideoEventValueChanged"/>

And Here is a snippet from my ConfigurationModel:

public bool DontUseSensorLength
{
    get { return !UseSensorLength; }
}

public bool UseSensorLength
{
   get { return _useSensorLength; }
   set 
   { 
      _useSensorLength = value; 
      OnPropertyChanged("UseSensorLength"); 
      OnPropertyChanged("DontUseSensorLength");
   }
}

Here is the error message I am getting in my output window when running the app:

System.Windows.Data Error: 40 : BindingExpression path error: 'ConfigurationModel' property not found on 'object' ''IncrementingTextBox' (Name='video_length_textbox')'. BindingExpression:Path=ConfigurationModel.DontUseSensorLength; DataItem='IncrementingTextBox' (Name='video_length_textbox'); target element is 'IncrementingTextBox' (Name='video_length_textbox'); target property is 'IsEnabled' (type 'Boolean')

Remember, the 'UseSensorLength' property binding is working fine, but the 'DontUseSensorLength' binding is causing the above 'BindingExpression path error'.

解决方案

I wrote some other SO answer recently about how to read the binding errors so they make more sense. To summarize, add line breaks to your error message on the colons and semi-colons, and read it from the bottom up.

Your error message is:

  • System.Windows.Data Error: 40 :
    • BindingExpression path error: 'ConfigurationModel' property not found on 'object' ''IncrementingTextBox' (Name='video_length_textbox')'.
    • BindingExpression:Path=ConfigurationModel.DontUseSensorLength;
  • DataItem='IncrementingTextBox' (Name='video_length_textbox');
  • target element is 'IncrementingTextBox' (Name='video_length_textbox');
  • target property is 'IsEnabled' (type 'Boolean')

This can be read from the bottom up as:

  • The binding failing is the IsEnabled property of an element of type IncrementingTextBox (named video_length_textbox).

  • The DataItem (DataContext) of the element is an object of type IncrementingTextBox named video_length_textbox

  • The binding expression it is trying to find is ConfigurationModel.DontUseSensorLength

  • And the problem the binding is having is that the ConfigurationModel property is not found on the data context object IncrementingTextBox

So your DataContext for "video_length_textbox" is set to itself, and your IncrementingTextBox class does not have a public property called ConfigurationModel

Since I don't see you setting the DataContext for your IncrementingTextBox anywhere in your XAML, check out the code for your IncrementingTextBox class. The most likely case is you are setting the DataContext to itself in either the Constructor

this.DataContext = this;

or the XAML

DataContext="{Binding RelativeSource={RelativeSource Self}}"

这篇关于WPF 错误 40 BindingExpression 路径错误:在“对象"上找不到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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