WPF Error 40 BindingExpression path error: property not found on 'object'
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 typeIncrementingTextBox
(named video_length_textbox).The
DataItem
(DataContext
) of the element is an object of typeIncrementingTextBox
named video_length_textboxThe 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 objectIncrementingTextBox
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}}"
How to resolve a Binding Expression Path error in WPF?
These binding errors are not related to your DataGrid.
They indicate that you have 3 TextBoxes somewhere of the names fNameTbx
, lNameTbx
, and emailTbx
. A DataGrid does not generate it's items with a Name property, so it is not the one causing these binding errors.
When trying to read binding errors, it's best to break them up by semi-colons and read them backwards, as demonstrated here.
For example,
System.Windows.Data Error: 40 : BindingExpression path error: 'FirstName' property not found on 'object' ''MainViewModel' (HashCode=55615518)'. BindingExpression:Path=FirstName; DataItem='MainViewModel' (HashCode=55615518); target element is 'TextBox' (Name='fNameTbx'); target property is 'Text' (type 'String')
Can also be read as
- target property is 'Text' (type 'String')
- target element is 'TextBox' (Name='fNameTbx');
- DataItem='MainViewModel' (HashCode=55615518);
- BindingExpression path error: 'FirstName' property not found on 'object' ''MainViewModel' (HashCode=55615518)'. BindingExpression:Path=FirstName;
Meaning somewhere you have
<TextBox Name="fNameTbx" Text="{Binding FirstName}" />
Where the DataContext
of this TextBox is of type MainViewModel
. And MainViewModel
does not have a property of FirstName
.
I'd recommend searching your project for those names, or you could use a tool like Snoop to debug databindings and DataContext issues at runtime.
System.Windows.Data Error: 40 : BindingExpression path error: property not found on object
You have used Binding Path CheckforNewHubs
but it should be CheckForNewHubs
.
WPF/XAML Property not found on 'object'
That is a DataBinding Error
Easiest way to read those is break it up by the colons/semi-colons, and read it backwards
System.Windows.Data Error: 40 : BindingExpression path error: 'Sender'
property not found on 'object' ''Char' (HashCode=6619237)'.
BindingExpression:Path=Sender; DataItem='Char' (HashCode=6619237);
target element is 'TextBlock' (Name=''); target property is 'Text'
(type 'String')
- target property is 'Text' (type 'String')
- target element is 'TextBlock' (Name='');
- BindingExpression:Path=Sender;
- DataItem='Char' (HashCode=6619237);
- 'Sender' property not found on 'object' ''Char' (HashCode=6619237)'.
- BindingExpression path error:
- System.Windows.Data Error: 40 :
1 tells you that there is a Text
property causing the error
2 tells you that the Text property is on a <TextBlock>
element
3 tells you the binding express causing the problem is {Binding Path=Sender}
4 tells you the DataItem/DataContext behind the <TextBlock>
element is an item of data type Char
5 tells you the actual problem with this: there is no property named Sender
on the object of type Char
6 just tells you it's a binding error
7 I have no idea what it means
Since I see you have a public property named Sender
on your Message
class, and its clear that Message
is not Char
, its obvious that your DataContext
for each item is wrong.
Since it is set to a Char
the most likely cause is you are binding to a string, and the DataContext
for each element is a character in that string.
And sure enough, ItemsSource="{Binding Source=Messages}
means you are changing the binding's Source
property from the current DataContext
to a string
. And strings are just character arrays, so it means you are binding to the character array { M, e, s, s, a, g, e, s }
If you change the Source
property to the Path
property, then it will correctly read DataContext.Messages
instead, and should work.
<ListView ItemsSource="{Binding Path=Messages}" ... />
(The word Path
here is optional, since if you don't specify a property name then the binding assumes it is the value for the Path
property)
As a side note, I don't see you setting your
DataContext
anywhere on the form, and I don't see a public Messages
property either.The MainWindow
constructor should probably have a line of code that looks like this to set the DataContext
to itself :
this.DataContext = this;
And you probably need a public property for your ObservableCollection<Message> messages
so the ListView
binding can find it :
public ObservableCollection<Message> Messages
{
get { return messages; }
set { messages = value; }
}
I'm not sure if these were merely overlooked, or if you didn't know you needed them.
Oh, and if you plan on changing any of these bound properties and having the UI automatically update, you'll want to implement INotifyPropertyChanged too :)
And since I'm in tutorial-mode here, I figured I should also link to this answer :
Transitioning from Windows Forms to WPF
I would highly recommend reading through it (and the linked articles) if you're new to how WPF works, and are switching from Winforms to WPF. Which it sounds like you are :)
BindingExpression path error
Your DisplayMemberPath
binding is causing the error, and in your case should be removed entirely since it is not needed.
To use DisplayMemberPath
, you need to be able to reference the property like ListView.ItemsSource[X].SomeProperty
, where SomeProperty
would be your DisplayMemberPath
You are getting this error because your ItemsSource
is a List<String>
, and String
does not contain a property called CategoryModel
.
To explain the exact binding error you have:
System.Windows.Data Error: 40 : BindingExpression path error: 'CategoryModel'
property not found on 'object' ''String' (HashCode=-57655201)'.
BindingExpression:Path=CategoryModel.CategoryList; DataItem='String'
(HashCode=-57655201); target element is 'TextBlock' (Name=''); target property is
'Text' (type 'String')
This line means it can't find the property
CategoryModel
on the objectString
BindingExpression path error: 'CategoryModel'
property not found on 'object' ''String' (HashCode=-57655201)'This line contains the
Path
property for the binding expression that is throwing the errorBindingExpression:Path=CategoryModel.CategoryList;
This line tells you the Source object for the binding that is throwing the error (typically the
DataContext
)DataItem='String'
(HashCode=-57655201);And this line means it is failing to bind the property
Text
on aTextBox
(DisplayMemberPath
is a shortcut way of making theItemTemplate
a singleTextBlock
, with it'sText
bound to theDisplayMemberPath
property)target element is 'TextBlock' (Name=''); target property is
'Text' (type 'String')
So to put it all together, it is telling you that it is trying to bind TextBox.Text
to {Binding Path=CategoryModel.CategoryList}
, however the DataContext
behind the TextBox
is of type String
, and String
does not have a property called CategoryModel
BindingExpression path error: property not found on 'object'
You don't want to set the DataContext on the UserControl. Instead, you want to set it in the scope of the UserControl.
Usually you do this in the constructor of the UserControl. I usually add a line like this:
this.RootElement.DataContext = myData;
Where RootElement is the first sub-element (the Content) of your UserControl (usually a panel like Grid or StackPanel).
In your case it would be:
this.lsvwOutput.DataContext = FindResource("myDataSource")
;
And makes sure that it's after the InitializeComponent() call.
It's just a question of scoping. You set the datacontext on the root panel of the usercontrol. This is a really non-obvious part of WPF.
UPDATE: As Markus points out below, in the case of a listview, you want to set an array of data, not just a data point. Take that into consideration when setting the DataContext in your constructor.
BindingExpression path error: property not found
In the UserControl definition you have the following line:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
It means that the DataContext for that control is the control itself. So WPF is trying to find the IsProgress property in this control.
As I understand, the IsProgress property is defined in the ViewModel of your Page.
So the best option to fix it is just to remove above line.
Additionaly to fix Width and Height bindings in control add RelativeSource={RelativeSource AncestorType={x:Type UserControl}}
to them.
Related Topics
How to Throttle Requests in a Web API
How to Sort Depended Objects by Dependency
Record Video of Screen Using .Net Technologies
Find Methods That Have Custom Attribute Using Reflection
Differencebetween Casting and Coercing
How to Import from Excel to a Dataset Using Microsoft.Office.Interop.Excel
Memorycache Does Not Obey Memory Limits in Configuration
How Can One Generate and Save a File Client Side Using Blazor
Threadpool.Queueuserworkitem VS Task.Factory.Startnew
When to Use Properties Instead of Functions
How to Read and Write Id3 Tags to an Mp3 in C#
Linq to SQL Where Clause Optional Criteria
Dependency Injection - New Instance Required in Several of a Classes Methods
Adding and Removing Anonymous Event Handler
How to Create an Xml Document Using Xmldocument
Wpf - Set Focus When a Button Is Clicked - No Code Behind