ItemsControl的ItemTemplate中绑定 [英] ItemsControl ItemTemplate Binding

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

问题描述

在WPF4.0,我有一个包含其他类类型的属性(相结合的多种数据类型的显示)的一类。是这样的:

 公共部分类业主
{
     公共字符串OWNERNAME {获得;组; }
     公众诠释OWNERID {获得;组; }
}

部分类ForDisplay
{
    公有制OwnerData {获得;组; }
    公众诠释信用{获得;组; }
}
 

在我的窗口,我有一个ItemsControl具有以下(限幅为清楚起见):

 < ItemsControl中的ItemsSource = {结合}>
    < ItemsControl.ItemTemplate>
        <的DataTemplate>
          <地方:MyDisplayControl
                OWNERNAME = {结合OwnerData.OwnerName}
                信用= {绑定信用卡} />
        < / DataTemplate中>
    < /ItemsControl.ItemTemplate>
< / ItemsControl的>
 

然后我得到的数据层显示信息的收集,并设置的DataContext 的ItemsControl 来这个集合。 信用属性获取正确显示,但OWNERNAME属性不会。相反,我得到一个绑定错误:

  

错误40:BindingEx pression路径   错误:OWNERNAME属性未找到   在'对象'''F​​orDisplay   (哈希code = 449124874)'。   BindingEx pression:路径= OWNERNAME;   DataItem的='ForDisplay   (哈希code = 449124874);目标元素   是的TextBlock'(名称= txtOwnerName');   目标属性是文本(类型   '串')

我不明白这是为什么试图去寻找OWNERNAME财产在ForDisplay类,而不是从ForDisplay OwnerData财产的所有者类。

修改 看来,它是与使用自定义的控制。如果我绑定了相同的属性到的TextBlock ,它们都能正常工作。

 < ItemsControl中的ItemsSource = {结合}>
    < ItemsControl.ItemTemplate>
        <的DataTemplate>
          < StackPanel的>
              <地方:MyDisplayControl
                        OWNERNAME = {结合OwnerData.OwnerName}
                        信用= {绑定信用卡} />
              < TextBlock的文本={结合OwnerData.OwnerName}/>
              < TextBlock的文本={结合信用}/>
          < / StackPanel的>
        < / DataTemplate中>
    < /ItemsControl.ItemTemplate>
< / ItemsControl的>
 

解决方案

您确定code你贴这里是code,你在你的解决方案中使用?因为,这code对我的作品:

XAML

 < ItemsControl中的ItemsSource ={结合}>
    < ItemsControl.ItemTemplate>
        <的DataTemplate>
            < StackPanel的>
                < TextBlock的文本={结合OwnerData.OwnerName}>< / TextBlock的>
                < TextBlock的文本={结合信用}/>
            < / StackPanel的>
        < / DataTemplate中>
    < /ItemsControl.ItemTemplate>
< / ItemsControl的>
 

窗口的Loaded事件

 的ObservableCollection< ForDisplay>项目=新的ObservableCollection< ForDisplay>();

的for(int i = 0;我小于10;我++)
{
    items.Add(新ForDisplay(){OwnerData =新用户(){OWNERID = I + 1,OWNERNAME =的String.Format(所有者#{0},我+ 1)},信用= I + 1});
}

的DataContext =项目;
 

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>

解决方案

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>

Window's Loaded Event

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天全站免登陆