WPF元素绑定
问题描述:
我有我的UserControl
内两个控件,我绑定到使用元素完全相同的对象绑定:WPF元素绑定
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;
}
答
我敢打赌,它的MainGrid这是绑定源是不是在你的约束性目标的逻辑树。
答
这是绑定元素的绝对问题。但是,您没有提供源代码,因此您想调试它并解决问题。
请参考下面的网址和“Cannot find source for binding with reference
”部分,明确说明了如何调试和解决它。
http://www.codeproject.com/Articles/244107/Debugging-WPF-data-bindings
请问您可以发布您的UserControl的源?我的第一个猜测是,这可能是一个DataContext问题? – Dutts 2013-02-18 08:16:24
我更新了我的问题! – 2013-02-18 08:37:09