项目控制项目模板绑定
在WPF4.0中,我有一个包含其他类类型作为属性的类(组合多种数据类型进行显示)。类似于:
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 的
到这个集合。 “Credit”属性显示正确,但 OwnerName 属性未正确显示。相反,我收到绑定错误:DataContext
错误 40:BindingExpression 路径 错误:找不到“OwnerName”属性 在“对象”“ForDisplay”上 (哈希码=449124874)'。 BindingExpression:路径=所有者名称; 数据项='用于显示' (哈希码=449124874);目标元素 是'TextBlock'(名称=txtOwnerName'); 目标属性是“文本”(类型 '字符串')
我不明白为什么它试图在 ForDisplay 类中查找 OwnerName 属性,而不是在 ForDisplay OwnerData 属性的 Owner 类中查找。
编辑 看来这与使用自定义控件有关。如果我将相同的属性绑定到 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您在此处发布的代码是您在解决方案中使用的代码吗?因为,这段代码对我有用:
XAML
Window's Loaded Event
Are you sure the code you posted here IS the code you use in your solution? Because, this code works for me :
XAML
Window's Loaded Event