C#WPF在一个列表框中显示多个表

时间:2018-01-23 11:35:57

标签: c# wpf visual-studio listbox

我有想法将数据库中的多个表绑定到一个listbox,但我不知道该怎么做。我有3个来源名为Boozes,Juices and Snacks。现在我只有列表框中显示的Boozes表。

我的MainWindow.Xaml代码:

 <ListBox Name="BoozeList" ItemsSource="{Binding Boozes}"
             Margin="10,124,0,10"
             HorizontalAlignment="Left"
             ScrollViewer.VerticalScrollBarVisibility="Visible"
              Width="233" Background="#FF79DCFA" BorderBrush="#FF0040FF">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}" />
                    <TextBlock Text="{Binding Path=UnitPrice}" />

                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="15"/>
            </Style>
        </ListBox.Resources>
    </ListBox>

2 个答案:

答案 0 :(得分:0)

将您的itemssource设置为复合集合:

<CollectionViewSource x:Key="BoozesCollection" Source="{Binding Boozes}"/>
<CollectionViewSource x:Key="JuicesCollection" Source="{Binding Juices}"/>
<CollectionViewSource x:Key="SnacksCollection" Source="{Binding Snacks}"/>

<CompositeCollection>
    <CollectionContainer Collection="{Binding Source={StaticResource BoozesCollection}}"/>
    <CollectionContainer Collection="{Binding Source={StaticResource JuicesCollection}}"/>
    <CollectionContainer Collection="{Binding Source={StaticResource SnacksCollection}}"/>
</CompositeCollection>

答案 1 :(得分:0)

将所有三个合并为一个表并绑定它。

dtAll.Merge(dtOne);
dtAll.Merge(dtTwo);
dtAll.Merge(dtThree);