WPF元素绑定

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; 
} 
+3

请问您可以发布您的UserControl的源?我的第一个猜测是,这可能是一个DataContext问题? – Dutts 2013-02-18 08:16:24

+0

我更新了我的问题! – 2013-02-18 08:37:09

我敢打赌,它的MainGrid这是绑定源是不是在你的约束性目标的逻辑树。

这是绑定元素的绝对问题。但是,您没有提供源代码,因此您想调试它并解决问题。

请参考下面的网址和“Cannot find source for binding with reference”部分,明确说明了如何调试和解决它。

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

+0

谢谢在发布之前,我确实看了一遍。这并没有帮助我找到解决方案。当“使用跟踪消息”我的所有属性等拼写正确,因为它们从工作的页面复制,也是转换器不起作用在这种情况下,作者承认,它是有限的! – 2013-02-18 09:18:07

+0

W:WizardPage> – Smaug 2013-02-18 09:23:26

+1

如果你有控制像上面会发生什么?我只是删除了第一个控件,我只有第二个控件。 – Smaug 2013-02-18 09:24:02