最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wpf - Pin a control to the bottom of another control, or the bottom of the window, whichever is higher - Stack Overflow

programmeradmin1浏览0评论

I have this scenario:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" Grid.Row="0">
        ...
    </StackPanel>
    <ScrollViewer
        Grid.Row="1"
        HorizontalScrollBarVisibility="Hidden"
        VerticalScrollBarVisibility="Auto">
        <ItemsControl ItemsSource="{Binding Settings}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
            ...
        </ItemsControl>
    </ScrollViewer>
    <Button Content="Add Row" Grid.Row="2" VerticalAlignment="Top"/>
</Grid>

I want the "Add Row" button to stay at the bottom of the ItemsControl, but I want that ItemsControl to start scrolling and the "Add Row" button to be pinned to the bottom of the window (so always visible), whenever the number of rows would exceed the window bounds. I can pin the button to the bottom of the control with "auto" row size for row 1, and I can pin it to the bottom of the window and allow the itemscontrol to scroll with "*" row size, but I can't seem to get both simultaneously.

Is there a clean way to achieve this?

I did try to force the MaxHeight of the scrollviewer, but that got ugly fast.

I have this scenario:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" Grid.Row="0">
        ...
    </StackPanel>
    <ScrollViewer
        Grid.Row="1"
        HorizontalScrollBarVisibility="Hidden"
        VerticalScrollBarVisibility="Auto">
        <ItemsControl ItemsSource="{Binding Settings}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
            ...
        </ItemsControl>
    </ScrollViewer>
    <Button Content="Add Row" Grid.Row="2" VerticalAlignment="Top"/>
</Grid>

I want the "Add Row" button to stay at the bottom of the ItemsControl, but I want that ItemsControl to start scrolling and the "Add Row" button to be pinned to the bottom of the window (so always visible), whenever the number of rows would exceed the window bounds. I can pin the button to the bottom of the control with "auto" row size for row 1, and I can pin it to the bottom of the window and allow the itemscontrol to scroll with "*" row size, but I can't seem to get both simultaneously.

Is there a clean way to achieve this?

I did try to force the MaxHeight of the scrollviewer, but that got ugly fast.

Share Improve this question asked Feb 4 at 2:42 Andy WynnAndy Wynn 1,2738 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

adding VerticalAlignment="Top" for outer Grid achieves necessary pinning

<Grid VerticalAlignment="Top">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" Grid.Row="0">
        ...
    </StackPanel>
    <ScrollViewer
        Grid.Row="1"
        HorizontalScrollBarVisibility="Hidden"
        VerticalScrollBarVisibility="Auto">
        <ItemsControl ItemsSource="{Binding Settings}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
            ...
        </ItemsControl>
    </ScrollViewer>
    <Button Content="Add Row" Grid.Row="2" VerticalAlignment="Top"/>
</Grid>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论