This project is read-only.

[Version 2.0] BindingExpression Error Setting DataContext of Child Element

Jun 27, 2012 at 2:04 AM
Edited Jun 27, 2012 at 2:07 AM

I am receiving a BindingExpression error when setting the DataContext of a child control contained within a LayoutAnchorable.

The DataContext binding path is relative to the DataContext of the parent UserControl. Interestingly, the binding in fact works at run-time but still results in an error seen in the output window. The error suggests that the binding within the ChildView are being evaluated before its DataContext has been bound.

This only occurs if the child control is hosted in an AvalonDock panel.

Any ideas on why this is happening and if there is a workaround?

  -- Terrence

Here is an example:

The error:
BindingExpression path error: 'Name' property not found on 'object' 'ParentViewModel' ... BindingExpression:Path=Name; DataItem='ParentViewModel' ... target element is 'Button' (Name=''); target property is 'Content'
<Window x:Class="BindingIssue.MainWindow"
        DataContext="{Binding Source={StaticResource ParentViewModel}}">
                        <v:ChildView DataContext="{Binding ChildViewModel}" />
<UserControl x:Class="BindingIssue.ChildView"
             xmlns:x="" >
    <Button Content="{Binding Name}"/>
public class ParentViewModel
    public ParentViewModel()
        ChildViewModel = new ChildViewModel("Bob Jones");
    public ChildViewModel ChildViewModel { get; private set; }
public class ChildViewModel
    public ChildViewModel(string name) { Name = name; }
    public string Name { get; private set; }

Here is a similar issue though unrelated to AvalonDock:
WPF: Setting DataContext of a UserControl with Binding not working in XAML