如何在wpf中水平绑定我的ui元素?

时间:2010-06-04 09:48:31

标签: wpf

                                                                                                                                                                                                                                                                                                                                                                                                                        
                                     我想在wpf中使用这些uielements创建水平列表。我已经使用了listview,如

<ListView Name="marqueeList" Height="300" Width="1000"  ItemsSource="{Binding}">
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel Orientation="Horizontal">                                        
                                    <VirtualizingStackPanel Orientation="Vertical">
                                      <VirtualizingStackPanel>
                                            <Label Name="CompanyName" FontWeight="Bold" Content="{Binding CompanyName}"></Label>
                                        </VirtualizingStackPanel>
                                        <VirtualizingStackPanel Orientation="Horizontal">
                                            <Label Name="CurrentPrice" Content="{Binding CurrentPrice}" ></Label>
                                            <Label Name="Deviation" Content="{Binding Deviation}"></Label>
                                        </VirtualizingStackPanel>
                                    </VirtualizingStackPanel>                                        
                                </VirtualizingStackPanel>
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>
                    </ListView>

但它抛出异常“ItemsPanelTemplate的VisualTree必须是单个元素。” 请帮助解决这个问题

1 个答案:

答案 0 :(得分:1)

要使列表视图水平,请执行此操作。

<ListView Name="marqueeList" ItemTemplate="{StaticResource myListViewTemplate}" ItemsSource="{Binding}">
    <ListView.ItemsPanel> 
        <ItemsPanelTemplate> 
            <VirtualizingStackPanel Orientation="Horizontal"></VirtualizingStackPanel> 
        </ItemsPanelTemplate> 
    </ListView.ItemsPanel>
</ListView>

您可以将所有其他内容放入DataTemplate

<DataTemplate x:Key="myListViewTemplate">
    <StackPanel> 
        <StackPanel>
            <Label Name="CompanyName" FontWeight="Bold" Content="{Binding CompanyName}"/>
        </StackPanel>
        <StackPanel> 
            <Label Name="CurrentPrice" Content="{Binding CurrentPrice}"/> 
            <Label Name="Deviation" Content="{Binding Deviation}"/>
        </StackPanel> 
     </StackPanel>
</DataTemplate>