开发者

Use two different LINQ-To-Entity objects as ItemSource for ListView in WPF

I am using LINQ to Entites where I have two table objects (ReferenceCodes and Stock) with relationships.

I need to combine both as seperate columns in a ListView like this:

XAML

   <ListView Name="myListView">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Reference Code"/>
                <GridViewColumn Header="Description"/>
            </GridView>
        </ListView.View>
    </ListView>

Using a LINQ statement I am able to set the itemsource of each table separately like this:

Code Behind

Dim Context as New myEntity

myStock = From s In Context.Stocks.ToList
          Select s.Description

myListView.ItemSource = myStock

But how do I set each column as a separate item source for each table? I have tried a join statement like t开发者_高级运维his:

Dim Context as New myEntity

myStock = From s In Context.Stocks.ToList
          Join r In Context.ReferenceCodes.ToList
          Select r.ReferenceDescription, s.Description

This seems to work except that it only displays a single column in this format:

{ ReferenceDescription = ABC1234 ; Description = My stock item description }

Instead of two separate columns, one a list of Reference Codes and the other a list of Stock Descriptions.

What am I doing wrong?


You probably just need to set the DisplayMemberBindings of the columns when you have that joined collection of anonymous objects:

<ListView Name="myListView">
     <ListView.View>
         <GridView>
             <GridViewColumn Header="Reference Code"
                             DisplayMemberBinding="{Binding ReferenceDescription}"/>
             <GridViewColumn Header="Description"
                             DisplayMemberBinding="{Binding Description}"/>
         </GridView>
     </ListView.View>
</ListView>
myListView.ItemsSource = myStock
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜