自动调整堆栈面板到父高度,其中列表视图在内部,带有scrollview

时间:2015-04-06 17:22:54

标签: wpf xaml listview scrollview expander

我是wpf的新手,我试图在每个扩展器上创建两个包含listview的扩展器,在标题中我创建了一个searchtext框并且工作正常。 问题是我无法创建两个扩展器的scrollview并使stackpanel高度适合父控件。 如何在没有文本框的情况下创建滚动视图到列表视图?

代码:

  <StackPanel Orientation="Vertical">
    <TextBox Margin="3,3,3,3" FontSize="16" Height="25" Name="searchTextBox" TextChanged="SearchTextBox_OnTextChanged" Grid.Row="0"></TextBox>
    <Border CornerRadius="6" BorderBrush="Gray" BorderThickness="1" DockPanel.Dock="Top" Grid.Row="1">
        <StackPanel Orientation="Vertical">
            <Expander IsExpanded="True" Background="LightGray" Grid.Row="0" >
                <Expander.Header>
                        <TextBlock Text="A" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                </Expander.Header>
                <Expander.Content>
                    <ListView Name="RecentEngines" BorderThickness="0" >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Expander.Content>
            </Expander>

            <Expander IsExpanded="True" Background="LightGray" Grid.Row="1">
                <Expander.Header>
                    <TextBlock Text="B" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                </Expander.Header>
                <Expander.Content>

                    <ListView Name="Engines" BorderThickness="0" MaxHeight="300">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

                </Expander.Content>
            </Expander>

        </StackPanel>
    </Border>

</StackPanel>

1 个答案:

答案 0 :(得分:0)

对于这个问题的评论,使用网格而不是StackPanels可能会更容易一些。

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBox Name="searchTextBox"
            Grid.Row="0"
            Height="25"
            Margin="3,3,3,3"
            FontSize="16" />
    <Border Grid.Row="1"
        BorderBrush="Gray"
        BorderThickness="1"
        CornerRadius="6">
        <ScrollViewer Height="300">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Expander Grid.Row="0"
                        Background="LightGray"
                        IsExpanded="True">
                    <Expander.Header>
                        <TextBlock VerticalAlignment="Bottom"
                                FontSize="22"
                                FontWeight="Bold"
                                Foreground="Gray"
                                Text="A" />
                    </Expander.Header>
                    <Expander.Content>
                        <ListView Name="RecentEngines" BorderThickness="0">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock FontWeight="Bold" Text="{Binding Name}" />
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </Expander.Content>
                </Expander>

                <Expander Grid.Row="1"
                        Background="LightGray"
                        IsExpanded="True">
                    <Expander.Header>
                        <TextBlock VerticalAlignment="Bottom"
                                FontSize="22"
                                FontWeight="Bold"
                                Foreground="Gray"
                                Text="B" />
                    </Expander.Header>
                    <Expander.Content>
                        <ListView Name="Engines"
                                MaxHeight="300"
                                BorderThickness="0">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock FontWeight="Bold" Text="{Binding Name}" />
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

                    </Expander.Content>
                </Expander>

            </Grid>
        </ScrollViewer>
    </Border>
</Grid>

我还拉了一些多余的标记元素。你想尽可能地避免这种情况,因为它会对深层嵌套元素造成严重破坏。 &#39; Grid.Row&#39;这里的元素是多余的,因为它在StackPanel中:

<Expander IsExpanded="True" Background="LightGray" Grid.Row="0" >