WPF:数据绑定查找 [英] WPF : Databinding lookup

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

问题描述

我是WPF的新手,有一个我无法解决的数据绑定问题.谷歌搜索无济于事.

有2个收藏集
1)MainCol:具有2个属性:Id和Name.
2)DetailsCol:具有3个属性:Id,描述和金额.
对于MainCol上的每条记录,在DetailsCol中只有一个记录由ID列标识.由于各种原因,我无法对MainCol进行描述和确定金额.

我有一个绑定到MainCol的项目控件.有一个数据模板,其中有两个显示ID和Name的文本框.如何从DetailsCol中获取相应的描述和金额值,并将其与ID和名称一起显示.

I am new to WPF and have a data binding problem that i am not able to solve. Searching of google didn''t help.

There are 2 collections
1) MainCol : has 2 properties: Id and Name.
2) DetailsCol : has 3 properties: Id, Description and Amount.
For each record on MainCol there is exactly one record in DetailsCol identified by Id column. For various reasons i am not able to Description and Amount to MainCol.

I have an itemscontrol that is bound to a MainCol. There is a datatemplate that has 2 textboxes that display Id and Name. How do get the corresponding value Description and Amount from DetailsCol and show it along with Id and Name.

<ItemsControl ItemsSource="{Binding Path=MainCol}" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Id}" ></TextBlock>
            <TextBlock Text="{Binding Path=Name}" ></TextBlock>
            <!-- How to show Description and Amount here by looking up the Id value -->
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>



注意:-MainCol有5个项目,而DetailsCol有10个项目.我只想在MainCol中显示这5个项目.



Note:- MainCol has 5 items and DetailsCol has 10 items. I want to show only those 5 items in MainCol.

推荐答案

在这种情况下,您需要将MultiBinding与Converters结合使用.

您的案子会很复杂.但是MultiBinding是要走的路.

-添加MultiBinding并绑定MainCol和DetailCol
-编写一个Converter来读取两者并作为具有您想要的属性的某些类返回,

FinalData类:
-ID(MainCol)
-名称(MainCol)
-说明(DetailCol)
-金额(DetailCol)


参考文献:
Silverlight MultiBindings,如何将多个绑定附加到单个属性. /a> [ ^ ]

即使在Silverlight中,概念也是相同的.
You need to use MultiBinding with the combination of Converters in this case.

It would be complex on your case. But MultiBinding is the way to go.

- Add MultiBinding and binding MainCol and DetailCol
- Write a Converter to read both and return as some class which has properties of what you want like,

FinalData class:
- ID (MainCol)
- Name (MainCol)
- Description (DetailCol)
- Amount (DetailCol)


References:
Silverlight MultiBindings, How to attach multiple bindings to a single property.[^]

Even though it is in Silverlight, the concept is same.


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

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