Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

c# - WPF Error 40 BindingExpression path error: property not found on 'object'

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'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...