无法使用stackpanel显示ScrollViewer

时间:2016-09-02 16:16:14

标签: c# wpf

我正在使用ListBox而我正在尝试显示滚动查看器,但我需要使用堆叠面板,因为我需要在水平模式下对齐项目,请参阅代码:

<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之外以使其正常工作。不幸的是,滚动查看器出现但似乎已禁用,似乎无法正常工作,因为我应该能够在内部滚动项目,但我无法做到。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

由于ScrollViewer已设置DataTemplate,因此您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>

编辑:ScrollBar现在可见且有效。 http://imgur.com/a/t2WPa