列表框中的数据绑定工具提示

时间:2009-03-17 17:20:33

标签: c# .net wpf xaml

我正在将xml结构绑定到列表框。我很困惑这样做。如何在datatemplate中放置一个datatemplate或者我需要使用一个hirarchialdatatemplate ...例如从xml,我想在列表框中显示汽车的Make Name,我想要将相应的Suvs显示为工具提示或上下文菜单。如何做到这一点..请帮助..任何输入都将非常有用..我的xml文件结构如下所示

<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 个答案:

答案 0 :(得分:1)

我修改了您的XML格式以支持多个汽车组:

        <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>

我连接了上下文菜单和工具提示。 下面是我如何连接绑定:

<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>