Click here to Skip to main content
15,995,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>


Note:- MainCol has 5 items and DetailsCol has 10 items. I want to show only those 5 items in MainCol.
Posted
Updated 12-Jan-11 14:14pm
v6
Comments
AspDotNetDev 12-Jan-11 17:02pm    
Are these "collections" in the database? What part are you having trouble with? Can't you just add 2 new properties to your object: Description and Amount? Why store a 1 to 1 object in a different place?
lejomonc 12-Jan-11 20:13pm    
yes, both collections are in database, but populated to List<customobject>. For performance reasons i am not able to add Description and Amount to MainCol.
Issue is, when Id and Name is getting rendered dynamically, how do i use the value of Id to lookup DetailsCol to get the corresponding Description and Amount. What should be the approach and syntax of binding.

1 solution

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.
 
Share this answer
 
Comments
lejomonc 14-Jan-11 3:30am    
Thank you for reponse. Instead of returning the an object that combines data from both collection, I returned the data from DetailsCol alone. Now the problem is since the datacontext has changed due to multi binding, how do i refer to the original datacontext
-------------------------------------------------------------------
<itemscontrol itemssource="{Binding Path=MainCol}">
<itemscontrol.itemtemplate>
<datatemplate>
<stackpanel>
<stackpanel.datacontext>
<multibinding converter="{StaticResource customConverter}">
<binding path="Id"></binding>
<binding path="DataContext.DetailsCol" relativesource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" />

</multibinding>
</stackpanel.datacontext>

<textblock text="{Binding Path=Id}"></textblock>
<!-- How to get the bindg for Name here -->
<textblock text="{Binding Path=Description}"></textblock>
<textblock text="{Binding Path=Amount}"></textblock>
</stackpanel></datatemplate>
</itemscontrol.itemtemplate>
</itemscontrol>
-------------------------------------------------------------------

I tried
<textblock text="{Binding Path=ItemsSource.CurrentItem.Name, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"></textblock>

But this always returns Name corresponding to the value the first item in MainCol. Not the one corresponding to the loops current item. How do i get the original databinding context?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900