WPF按钮位置依赖于另一个元素

时间:2017-05-11 12:27:08

标签: wpf xaml user-interface data-binding

我想将我的按钮始终放在ScrollViewer底部框架的中间位置。我要更改窗口的大小和ScrollViewer的大小,但我希望我的按钮始终与图片一样。

enter image description here 由于我正在关注MVVM,我只有xaml。基本上,我想从模式中绑定(实时)按钮顶部位置:

button.top = (scrollViewer.top + scrollViewer.height) - button.height/2

我很感激你的建议。

[编辑]我忘了添加所有其他控件都在网格行和列中。

1 个答案:

答案 0 :(得分:2)

您可以尝试使用Grid来实现这一目标。如果您需要更改ScrollViewer尺寸,请改为更改ScrollGrid网格尺寸。要重叠底部或顶部内容,您可以使用按钮的负边距。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="100"/>
    </Grid.RowDefinitions>
    <Border Background="Red"/>
    <Border Background="Red" Grid.Row="2"/>
    <Grid x:Name="ScrollGrid" Grid.Row="1">
        <ScrollViewer></ScrollViewer>
        <Button Width="100" Height="100" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
    </Grid>
</Grid>