1

我确信这很简单,但我没有找到解决方案。如何使用一个 ListBox 绑定到数据源中的两个数组?

以下是数据示例:

    <XmlDataProvider x:Key="ConfigurationData" XPath="Configuration/Component">
    <x:XData>
        <Configuration xmlns="">
            <Component ID="2252371">
                <ComponentAttribute ID="301080453">
                    <Name>ColorHexCodes</Name>
                    <Value />
                    <Values>
                        <Value>#FFFFFA</Value>
                        <Value>#FFFFFA</Value>
                        <Value>#FFFFFA</Value>
                        <Value>#FFFFFA</Value>
                        <Value>#FFFFFA</Value>
                        <Value>#FFFFFA</Value>
                        <Value>#A80000</Value>
                        <Value>#A80000</Value>
                        <Value>#A80000</Value>
                        <Value>#A80000</Value>
                        <Value>#A80000</Value>
                        <Value>#A80000</Value>
                        <Value>#D1D3D4</Value>
                        <Value>#D1D3D4</Value>
                        <Value>#D1D3D4</Value>
                        <Value>#D1D3D4</Value>
                        <Value>#D1D3D4</Value>
                        <Value>#D1D3D4</Value>
                    </Values>
                </ComponentAttribute>
                <ComponentAttribute ID="301080500">
                    <Name>ColorDescription</Name>
                    <Value />
                    <Values>
                        <Value>0010 - White</Value>
                        <Value>0010 - White</Value>
                        <Value>0010 - White</Value>
                        <Value>0010 - White</Value>
                        <Value>0010 - White</Value>
                        <Value>0010 - White</Value>
                        <Value>1902 - Red</Value>
                        <Value>1902 - Red</Value>
                        <Value>1902 - Red</Value>
                        <Value>1902 - Red</Value>
                        <Value>1902 - Red</Value>
                        <Value>1902 - Red</Value>
                        <Value>3971 - Silver</Value>
                        <Value>3971 - Silver</Value>
                        <Value>3971 - Silver</Value>
                        <Value>3971 - Silver</Value>
                        <Value>3971 - Silver</Value>
                        <Value>3971 - Silver</Value>
                    </Values>
                </ComponentAttribute>
            </Component>
        </Configuration>
    </x:XData>
</XmlDataProvider>

我需要显示的是并排显示 ColorDescription 和 ColorHexCodes 的列表。这两个系列将始终排列。

我想出了这个:

    <ListBox Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="Auto" Width="Auto" Grid.ColumnSpan="2">
    <ListBox.ItemsSource>
        <Binding Source="{StaticResource ConfigurationData}" XPath="//ComponentAttribute[Name='ThreadDescription']/Values/*" />
    </ListBox.ItemsSource>
    <ListBox.ItemTemplate>
        <DataTemplate x:Key="Swatch">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                <TextBlock Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" Height="20" FontSize="14" Foreground="CadetBlue">
                    <TextBlock.Text>
                        <Binding XPath="//ComponentAttribute[Name='ColorDescription']/Values/Value" />
                    </TextBlock.Text>
                </TextBlock>
                <TextBlock Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" Height="20" Margin="20,0,0,0" FontSize="14" Foreground="CadetBlue">
                    <TextBlock.DataContext></TextBlock.DataContext>
                    <TextBlock.Text>
                        <Binding XPath="//ComponentAttribute[Name='ColorHexCodes']/Values/Value" />
                    </TextBlock.Text>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

它呈现正确数量的 ListBoxItem,但仅显示每个项目中的第一个值。我哪里错了?我知道这一定很容易,我只是想念。

4

1 回答 1

0

研究使用可以容纳两个列表的复合集合存储,但用于被一个列表框绑定并专门处理每种项目类型

MSDN 的链接给出了在一个显示器中显示两个不同数据项的示例。

于 2015-02-26T22:56:16.760 回答