在WPF4.0中,我有一個包含其他類類型作爲屬性的類(組合多個數據類型以供顯示)。喜歡的東西:ItemsControl ItemTemplate Binding
public partial class Owner
{
public string OwnerName { get; set; }
public int OwnerId { get; set; }
}
partial class ForDisplay
{
public Owner OwnerData { get; set; }
public int Credit { get; set; }
}
在我的窗口,我有一個ItemsControl具有以下(限幅爲清楚起見):
<ItemsControl ItemsSource={Binding}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:MyDisplayControl
OwnerName={Binding OwnerData.OwnerName}
Credit={Binding Credit} />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
然後我得到的數據層顯示信息的收集,並設置的ItemsControl
到這個集合。 「信用」屬性正確顯示,但OwnerName屬性沒有。相反,我得到一個綁定錯誤:
Error 40: BindingExpression path error: 'OwnerName' property not found on 'object' ''ForDisplay' (HashCode=449124874)'. BindingExpression:Path=OwnerName; DataItem='ForDisplay' (HashCode=449124874); target element is 'TextBlock' (Name=txtOwnerName'); target property is 'Text' (type 'String')
我不明白這是爲什麼試圖去尋找OWNERNAME財產在ForDisplay類,而不是從ForDisplay OwnerData財產所有者類。
編輯 它似乎與使用自定義控件有關。如果我將相同的屬性綁定到TextBlock
,則它們可以正常工作。
<ItemsControl ItemsSource={Binding}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<local:MyDisplayControl
OwnerName={Binding OwnerData.OwnerName}
Credit={Binding Credit} />
<TextBlock Text="{Binding OwnerData.OwnerName}" />
<TextBlock Text="{Binding Credit}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
不知何故,它試圖找到ForDisplay對象的OWNERNAME財產。使用以下鏈接進行調試:http://wpftutorial.net/DebugDataBinding.html。還有一個問題,MyDisplayControl上的OwnerName屬性是一個依賴項屬性? – decyclone 2010-06-17 15:29:22
我沒有看到任何錯誤,假設你修剪的東西沒有隱藏任何相關的東西。您是否嘗試過清潔/重建解決方案?我討厭這個建議,因爲它相當於告訴你「重新啓動」,但我有一個數據綁定情況應該工作,但不是,我發現一個乾淨/重建有時會修復它。不知道爲什麼。 – 2010-06-17 15:37:17
初步猜測:更改綁定以顯式設置路徑。即「OwnerName = {綁定路徑= OwnerData.OwnerName}」 – 2013-06-11 01:08:24