【发布时间】:2013-02-18 08:15:31
【问题描述】:
我的UserControl 中有两个控件,我使用元素绑定绑定到完全相同的对象:
AllowNext="{Binding ElementName=MainGrid, Path=DataContext.CanContinue}"
在第一个控件上它工作正常,但在第二个控件上我得到一个绑定异常:
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')
我也尝试在第二个控件上使用RelativeSource 绑定:
AllowNext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.CanContinue}"
但这也给了我一个错误。
有谁知道这可能是什么?
--
这是简化的控件:
<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>
现在正如我所提到的,MainPage 绑定很好,而 DeductionPage 根本没有绑定并得到提供的错误。 MainGrid 的 DataContext 是从后面的代码中设置的:
public void SetDataContext(object o)
{
MainGrid.DataContext = o;
}
【问题讨论】:
-
能否请您发布您的 UserControl 的源代码?我的第一个猜测是这可能是 DataContext 问题?
-
我更新了我的问题!