views:

68

answers:

2

I have a wpf tabitem whose data context is set to my object 'Product'. All the controls on this form get their data from 'Product' object. I have a listview whose ItemsSource property is set to a list in my object 'Product.DetailsList'. Listview columns are bound to object properties in 'Product.DetailsList'

Up till here everything works fine. Now I need to bind some of the columns in my listview to the properties in my datacontext object i.e.'Product'. Can someone tell me how can i achieve this?

A: 

I think is not completely clear to me how the hierarchy between your controls is. DataContext works this way: is being inherited from the top level control to their children. In this case, if the WPF TabItem is the parent control of ListView, the ListView will have the same DataContext as the TabItem.

Also, assuming I get it right, you can do this:

       <TabItem>
            <ListView>
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding RelativeSource={RelativeSource FindAncestor, 
                                        AncestorType={x:Type TabItem}}, Path=DataContext.MyPropInProductObject}">

                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </TabItem>

where the interesting part is the Relative Source set to FindAncestor and since you know that the DataContext is set to the Product object, you can ask for the property you want there.

HTH

Markust
A: 

If I understood your question correctly, you ask how to bind to some property of current item in collection. It is done by using slash ("/") in binding path.

Here is example from MSDN:

<Button Content="{Binding }" />
<Button Content="{Binding Path=/}" />
<Button Content="{Binding Path=/Description}" />

In above example first row binds to collection, second row binds to current item of collection and third row binds to Description property of current item in collection.

Example is from MSDN article: Binding to Collections - look for Current Item Pointers.

zendar