69

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

Curtis
  • 5,794
  • 8
  • 50
  • 77
  • 1
    Can you post the error as well that you get from the Output Window? – Bob. Apr 23 '13 at 15:59
  • I have added the error message text to the above posting. – Curtis Apr 23 '13 at 16:03
  • 2
    You have to set the DataContext to reflect your current User Control. When you create the binding, it is looking inside `IncrementingTextBox`'s DataContext for the `ConfigurationModel.DontUseSensorLength` value. You have to use a `RelativeSource` similar to [this](http://stackoverflow.com/a/11550721/1466627). – Bob. Apr 23 '13 at 16:08

9 Answers9

143

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}}"
Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • 3
    Rachel, you are a beautiful genius!!! And I am an idiot. Thank you so much for the solution. I was binding to self and didn't have a 'ConfigurationModel' property in my IncrementingTextBox class. Again, thanks !!! For some reason, I just can't wrap my head around binding sometimes.. I try to use it all the time, but seem to run into dumb Curtis mistakes on a regular basis. Eventually I'll get it figured out.. – Curtis Apr 23 '13 at 16:31
  • 13
    @Curtis Don't worry, it gets easy once you understand how WPF works. You might be interested in reading a beginner blog post I have: [What is this "DataContext" you speak of?](http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/). To summarize, a WPF application has two layers: the data layer (the `DataContext`) and a UI layer. Bindings are used to pull data from the data layer into the UI layer, and the UI layer is really only meant to be a pretty user-friendly interface for the data layer. Once you learn to keep those layers separate, it becomes easy :) – Rachel Apr 23 '13 at 16:35
115

I had same problem because class of object from which I was pulling out data didn't have get; and set; on its properties.

this didn't work:

public string Name;

but this worked:

public string Name{ get; set; }
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Janiiik
  • 1,318
  • 1
  • 9
  • 7
11

I had a similar experience, the ItemsSource binding on a Combobox did not work.

In my case it was a minor mistake, but a difficult one to track until I enabled trace messages.

I simply forget to turn my List into a property :(

// NOPE:
public List<string> Versions;
// YEP:
public List<string> Versions { get; set; }

Maybe this helps someone...

uceumern
  • 877
  • 10
  • 14
5
public Window()
{
      InitializeComponent();
      this.DataContext = this;
}
public string Name {get;}
//xaml
<TextBlock Text="{Binding Name}"/>

Properties Name should be public and { get; }

  • I've spent couple hours of figuring out why does `Children` objects of my `Parent` node in TreeView are not displayed, and your answer helped me to realize, that I simply didn't have `get` accessor for `Children` object... – kosist Jul 03 '20 at 21:02
2

I had the same problem and in my case I was using bool instead of Boolean. As soon as I changed it, it's working as expected.

BlackM
  • 3,927
  • 8
  • 39
  • 69
2

Few things to check

1.assign values in properties before InitializeComponent in constructor

 public partial class SampleClass: UserControl
{
    public SampleClass()
    {
        ScenarioHeight = System.Windows.SystemParameters.WorkArea.Height - 350;
        InitializeComponent();           


    }

    public double ScenarioHeight  { get;set;}

2.if its a usercontrol make sure to add userControl as Element in the binding

 <ScrollViewer Name="sv" Height="{Binding Path=ScenarioHeight, ElementName=ucSampleClass}" >
Shahid
  • 51
  • 3
2

This error may also occur when you were previously trying to bind inaccessible or non-existing Enumerable instance using XAML property <ItemsSource>

When you correct the ItemsSource with the correct value XAML doesn't automatically reilitialize the collection of items.

So when I was using the ListBox UI - list representation I faced this in the properties:

enter image description here

Deleting all the items in collection and correcting ItemSource value was the key.

Max Bender
  • 372
  • 4
  • 14
1

After looking at Shahid's answer, I noticed in my case that I had set the DataContext to a reference in the Loaded event instead of in the constructor. Moving it to the constructor fixed the issue.

-2

I got this error and my case was as simple as setting the String I was binding to from private to public.

Careless mistake writing my backing field.

MrDevDevy
  • 17
  • 1
  • 3
    This statement is probably more of a comment than an answer, because it doesn't provide any specific resolution to the poster's question. – MichaelD Jul 17 '19 at 19:03