In WPF4.0, I have a class that contains other class types as properties (combining multiple data types for display). Something like:
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; }
}
In my window, I have an ItemsControl with the following (clipped for clarity):
<ItemsControl ItemsSource={Binding}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:MyDisplayControl
OwnerName={Binding OwnerData.OwnerName}
Credit={Binding Credit} />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I then get a collection of display information from the data layer, and set the DataContext
of the ItemsControl
to this collection. The "Credit" property gets displayed correctly, but the OwnerName property does not. Instead, I get a binding error:
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')
I don't understand why this is attempting to look for the OwnerName property in the ForDisplay class, rather than in the Owner class from the ForDisplay OwnerData property.
Edit
It appears that it has something to do with using the custom control. If I bind the same properties to a TextBlock
, they work correctly.
<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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…