Reputation: 930
I'm having some trouble binding to a certain dependency property on a business object in our application. We have many other bindings in place. I'm just not sure why this one does not work.
The UserControl XAML looks is basically what is shown below. This does not work and produces PathError
when I check the status.
<UserControl>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource BusinessObject}}">
<ListBox x:Name="DocumentDisplay" ItemContainerStyle="{DynamicResource ContainerStyle}" ItemsSource="{Binding Instance.ActiveDocument, Path=Paragraphs}" />
</Grid>
</UserControl>
I've checked in the C# code-behind and verified the following:
However, I am able to successfully create the binding using C#:
var dogPargBinding = new Binding() { Source = BusinessObjectClass.Instance.ActiveDocument, Path = new PropertyPath("Paragraphs") };
this.DocumentDisplay.SetBinding(ListBox.ItemsSourceProperty, dogPargBinding);
Even though this works, I'd like the binding to work from XAML, because much of our development is done through Expression Blend.
Some more details about our implementation:
I'm sure there is a simple explanation for what we're doing wrong, but it's getting to the point where it's time to ask. Thanks in advance.
=============================================================================
I finally found the problem I was having. The DependencyProperty was being declared with the wrong owner class type. That screwed up everything, and nothing VS did led me to the answer quickly.
The most helpful answer was one that got deleted... :(
Upvotes: 1
Views: 254
Reputation: 41393
Your Binding is incorrect. You have {Binding Instance.ActiveDocument, Path=Paragraphs}
, which is effectively setting the Path property twice.
The Instance.ActiveDocument
part doesn't specify a property before it, so it will use this Binding constructor, which sets the path of the binding.
Then you set the Path property, effectively overwriting the value passed to the constructor.
I think you mean to use {Binding Path=Instance.ActiveDocument.Paragraphs}
. Since your DataContext is an instance of BusinessObjectClass
, this path will look for the Instance property on it. Then it will look for the ActiveDocument property on the object returned from the Instance property. And finally, look for the Paragraphs property on the object returned from the ActiveDocument property.
Upvotes: 2