无法显示带有stackpanel的ScrollViewer

本文关键字:stackpanel ScrollViewer 显示 | 更新日期: 2023-09-27 18:05:28

我正在使用ListBox,我试图显示滚动查看器,但我需要使用stackpanel,因为我需要在水平模式下对齐我的项目,参见代码:

<ListBox VerticalAlignment="Stretch" 
                         Background="AliceBlue"
                         ScrollViewer.CanContentScroll="True" 
                         ScrollViewer.VerticalScrollBarVisibility="Visible"
                         ItemsSource="{Binding EventInfo}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Auto">
                            <StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Gara" FontWeight="Bold" Margin="0, 0, 50, 0"/>
                                    <TextBlock Text="{Binding League}" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Data" FontWeight="Bold" Margin="0, 0, 50, 0"/>
                                    <TextBlock Text="{Binding Date}" />
                                </StackPanel>
                                </StackPanel>
                            </StackPanel>
                        </ScrollViewer>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

你怎么能看到我把ScrollViewer外面的Stackpanel使其工作。不幸的是,滚动查看器出现,但似乎禁用,似乎不工作,因为我应该能够滚动项目里面,但我不能。我该如何解决这个问题?

无法显示带有stackpanel的ScrollViewer

DataTemplate中不需要ScrollViewer,因为ListBox已经有了ScrollViewer集。

另外,你应该更新你的DataTemplate使用Grid(它比堆叠StackPanels更干净)。

    <ListBox VerticalAlignment="Stretch" 
             Background="AliceBlue"
             ScrollViewer.CanContentScroll="True" 
             ScrollViewer.VerticalScrollBarVisibility="Auto"
             ItemsSource="{Binding EventInfo}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Gara" FontWeight="Bold" Margin="0, 0, 50, 0"
                               Grid.Column="0"
                               Grid.Row="0"/>
                    <TextBlock Text="{Binding League}" 
                               Grid.Column="1"
                               Grid.Row="0"/>
                    <TextBlock Text="Data" FontWeight="Bold" Margin="0, 0, 50, 0"
                               Grid.Column="0"
                               Grid.Row="1"/>
                    <TextBlock Text="{Binding Date}"
                               Grid.Column="1"
                               Grid.Row="1"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
编辑:滚动条现在是可见的和工作。http://imgur.com/a/t2WPa