1
votes

I have two controls within my UserControl where I bind to the exact same object using Element Binding:

AllowNext="{Binding ElementName=MainGrid, Path=DataContext.CanContinue}"

On the first control it works fine but on the second I get a binding exception:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MainGrid'. BindingExpression:Path=DataContext.CanContinue; DataItem=null; target element is 'WizardPage' (Name='DeductionPage'); target property is 'AllowNext' (type 'Boolean')

I have also tried using RelativeSource binding on the second control:

AllowNext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.CanContinue}"

But this also gives me an error.

Does anyone know what this might be?

--

Here is the simplified control:

<Grid Name="MainGrid">
    <w:Wizard Name="MyWizard" w:Designer.PageIndex="1" DataContext="{Binding ElementName=MainGrid, Path=DataContext.Policy}" >
        <w:WizardPage Header="Main Member" MaxHeight="600" AllowNext="{Binding ElementName=MainGrid, Path=DataContext.CanContinue}" Name="MainPage">
        </w:WizardPage>
        <w:WizardPage Name="DeductionPage" Header="Policy Details" AllowBack="False" AllowNext="{Binding ElementName=MainGrid, Path=DataContext.CanContinue}">
        </w:WizardPage>
    </w:Wizard>
</Grid>

Now as I mentioned, MainPage binds fine, whereas the DeductionPage does not bind at all and gets the supplied error. The DataContext of MainGrid is set from code behind:

    public void SetDataContext(object o)
{
  MainGrid.DataContext = o;
}
2
Could you please post the source for your UserControl? My first guess is that this may be a DataContext issue? - Dutts
I updated my question! - Chrisjan Lodewyks

2 Answers

0
votes

I bet it's the MainGrid which is the binding source is not in the logical tree of your binding target.

0
votes

This is the problem absolutely with the binding element. However, you didn't give the source so You want to debug it and solve the issue.

Refer the below url and "Cannot find source for binding with reference" section where explained obviously how to debug and solve it.

http://www.codeproject.com/Articles/244107/Debugging-WPF-data-bindings