在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