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?
Thanks
-- 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" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ad="http://avalondock.codeplex.com" xmlns:v="clr-namespace:BindingIssue" DataContext="{Binding Source={StaticResource ParentViewModel}}"> <ad:DockingManager> <ad:LayoutRoot> <ad:LayoutPanel> <ad:LayoutAnchorablePane> <ad:LayoutAnchorable> <v:ChildView DataContext="{Binding ChildViewModel}" /> </ad:LayoutAnchorable> </ad:LayoutAnchorablePane> </ad:LayoutPanel> </ad:LayoutRoot> </ad:DockingManager> </Window>
<UserControl x:Class="BindingIssue.ChildView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Button Content="{Binding Name}"/> </UserControl>
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