tags:

views:

461

answers:

2

Hi, I am working with binding an xml structure to a listbox. I am quite confused hoe to do this.How to put a datatemplate inside a datatemplate or i need to use a hirarchialdatatemplate...for example from the xml, I want to display the Make Name of the cars in a list box and i want to show the corresponding Suvs's as a tooltip or contextmenu.How to do this..please help..any input will be highly helpfull..my xml file structure is as given below

<XmlDataProvider x:Key="src">
    <x:XData>
        <Automobiles>
            <Id>24</Id>
            <Category>Cars</Category>
            <MakeName>Audi</MakeName>
            <Suvs>
                <SuvId>Item1</SuvId>
                <SuvId>Item1</SuvId>
                <SuvId>Item1</SuvId>
                <SuvId>Item1</SuvId>
            </Suvs>
            <IsPanel>1</IsPanel>
            <IsFav>1</IsFav>
        </Automobiles>
    </x:XData>
</XmlDataProvider>
+1  A: 

I modified your XML format to support multiple Automobile groups:

        <XmlDataProvider x:Key="src">
        <x:XData>
            <Automobiles xmlns="">
                <Automobile>
                    <Id>24</Id>
                    <Category>Cars</Category>
                    <MakeName>Audi</MakeName>
                    <Suvs>
                        <SuvId>audiItem1</SuvId>
                        <SuvId>audiItem2</SuvId>
                        <SuvId>audiItem3</SuvId>
                        <SuvId>audiItem4</SuvId>
                    </Suvs>
                    <IsPanel>1</IsPanel>
                    <IsFav>1</IsFav>
                </Automobile>
                <Automobile>
                    <Id>24</Id>
                    <Category>Cars</Category>
                    <MakeName>BMW</MakeName>
                    <Suvs>
                        <SuvId>bmwItem1</SuvId>
                        <SuvId>bmwItem2</SuvId>
                        <SuvId>bmwItem3</SuvId>
                        <SuvId>bmwItem4</SuvId>
                    </Suvs>
                    <IsPanel>1</IsPanel>
                    <IsFav>1</IsFav>
                </Automobile>
            </Automobiles>
        </x:XData>
    </XmlDataProvider>

I hooked up both a context menu and a tooltip. Below is how I wired up the bindings:

<ItemsControl ItemsSource="{Binding Source={StaticResource src}, XPath=/Automobiles/Automobile}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel.ContextMenu>
                            <ContextMenu ItemsSource="{Binding XPath=Suvs}">
                                <ContextMenu.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="SUV ID: " />
                                            <TextBlock Text="{Binding XPath=SuvId}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </ContextMenu.ItemTemplate>
                            </ContextMenu>
                        </StackPanel.ContextMenu>
                        <StackPanel.ToolTip>
                            <ListBox ItemsSource="{Binding XPath=Suvs/SuvId}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="SUV ID: " />
                                            <TextBlock Text="{Binding InnerText, StringFormat={}}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </StackPanel.ToolTip>
                        <TextBlock Text="Make: " />
                        <TextBlock Text="{Binding XPath=MakeName}" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
markti
A: 

thank u buddy....sorry for the late reply...that really helped out...sorry for the xml...it was my mistake..