dythim
dythim

Reputation: 930

Help with path error when databinding to a dependency property of a class instance

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:

  1. The DataContext is correct and non-null.
  2. The property is set and also non-null.

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:

  1. Just to reiterate, this same patten has worked in numerous other locations in the code.
  2. ActiveDocument is a dependency property of the BusinessObjectClass, and it is set from a multi-binding created in the C# code-behind. Testing shows this property is set correctly.

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

Answers (1)

CodeNaked
CodeNaked

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

Related Questions