2013-02-18 90 views
1

我有我的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

回答

0

我敢打賭,它的MainGrid這是綁定源是不是在你的約束性目標的邏輯樹。

0

這是綁定元素的絕對問題。但是,您沒有提供源代碼,因此您想調試它並解決問題。

請參考下面的網址和「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

<網格名稱= 「MainGrid」> Smaug 2013-02-18 09:23:26

+1

如果你有控制像上面會發生什麼?我只是刪除了第一個控件,我只有第二個控件。 – Smaug 2013-02-18 09:24:02