【问题标题】:WPF button position dependent on another elementWPF 按钮位置依赖于另一个元素
【发布时间】:2017-10-10 10:55:38
【问题描述】:

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

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

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

我会很感激你的建议。

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

【问题讨论】:

  • 在代码隐藏中设置 button.top 不会以任何方式破坏 MVVM。
  • 总是一样的ScrollViewer - 你的Button 被放置在哪里?

标签: wpf xaml user-interface data-binding


【解决方案1】:

您可以尝试使用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>

【讨论】:

  • 谢谢。它比我想象的要简单得多。它也适用于 Storyboard。
猜你喜欢
  • 1970-01-01
  • 2022-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 2013-01-05
相关资源
最近更新 更多