ItemsControl ItemTemplate 绑定 [英] ItemsControl ItemTemplate Binding

查看:22
本文介绍了ItemsControl ItemTemplate 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WPF4.0 中,我有一个包含其他类类型作为属性的类(组合多种数据类型进行显示).类似的东西:

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; }
}

在我的窗口中,我有一个 ItemsControl,其中包含以下内容(为清晰起见进行了剪辑):

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>

然后我从数据层获取了一个显示信息的集合,并将ItemsControlDataContext设置为这个集合.信用"属性得到正确显示,但 OwnerName 属性没有.相反,我收到了一个绑定错误:

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:

错误 40:BindingExpression 路径错误:找不到所有者名称"属性在'对象'''ForDisplay'(HashCode=449124874)'.BindingExpression:Path=OwnerName;数据项='显示'(哈希码=449124874);目标元素是 'TextBlock' (Name=txtOwnerName');目标属性是文本"(类型'字符串')

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')

我不明白为什么要尝试在 ForDisplay 类中查找 OwnerName 属性,而不是在来自 ForDisplay OwnerData 属性的 Owner 类中.

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.

编辑似乎与使用自定义控件有关.如果我将相同的属性绑定到 TextBlock,它们就可以正常工作.

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>

推荐答案

您确定您在此处发布的代码是您在解决方案中使用的代码吗?因为,这段代码对我有用:

Are you sure the code you posted here IS the code you use in your solution? Because, this code works for me :

XAML

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding OwnerData.OwnerName}"></TextBlock>
                <TextBlock Text="{Binding Credit}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

窗口的加载事件

ObservableCollection<ForDisplay> items = new ObservableCollection<ForDisplay>();

for (int i = 0; i < 10; i++)
{
    items.Add(new ForDisplay() { OwnerData = new Owner() { OwnerId = i + 1, OwnerName = String.Format("Owner #{0}", i + 1) }, Credit = i + 1 });
}

DataContext = items;

这篇关于ItemsControl ItemTemplate 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆