I'm looking for some general WPF/C# info on binding to a custom class using ObervableCollection. I currently have an error relating to "BindingExpression path error"..."property not found on". Any pointers would be good.
Asked
Active
Viewed 3,902 times
-6
-
1Show us some XAML & Code please. – Nicolas Repiquet Apr 17 '12 at 07:37
-
Might be a duplicate. See here: http://stackoverflow.com/questions/5382164/wpf-bindingexpression-path-error-property-not-found-on-object – McGarnagle Apr 17 '12 at 07:47
-
@dbaseman Thanks for the pointer, I was looking for more of a tutorial into why it works the way it does which is I didn't post any code, the link you added does point me in the right direction. – MAO Apr 17 '12 at 21:04
2 Answers
1
It sounds like you haven't assigned your DataContext. Below is a brief example.
Assuming your custom class looks something like this:
CODE:
public class Foo
{
private ObservableCollection<string> _names;
public ObservableCollection<string> Names
{
get{ return _names;}
set
{
_names = value;
}
}
}
and your XAML looks like
XAML:
<ListBox Name="lstNames" ItemsSource="{Binding Names}"/>
Set your DataContext in code behind.
lstNames.DataContext = new Foo();
This is a very simplistic version to achieve what you need. You should really have a look at Binding to Collections.

nickm
- 1,775
- 1
- 12
- 14
-
This is pointing me in the right direction but I am still struggling, thanks for the help – MAO Apr 17 '12 at 22:06
-
@MarkOates, the DataContext is used by WPF to locate the property you are trying to bind. In the case above for example, the list box knows that it's DataContext is an instance of Foo. The binding source for the list box is Names (specified in ItemsSource). WPF then knows to go to Foo then iterate down the property tree to find the property called Names. When it finds it it will subscribe to an event that is raised whenever a change is made to the ObservableCollection. – nickm Apr 17 '12 at 22:57
0
There are two reasons, that may appears. First - you have typed property name on xaml with error. Second - you forgot to set DataContext
to your View
.

stukselbax
- 5,855
- 3
- 32
- 54
-
or he instantiates binding in code-behind, and in this case it's better to use Binding.Source to set source. – EvAlex Apr 17 '12 at 07:55