如何在WPF中锚定控件?

发布于 2024-10-20 15:13:01 字数 146 浏览 1 评论 0原文

我有一个 TreeView 填充应用程序的顶部,但由于 TreeView 中的项目数量发生变化,我的“应用”按钮会垂直更改其位置。有没有办法将其锚定到窗口的底部,因此它始终位于窗口的右下部分,即距右下边缘 10x10 的距离。

I have a a TreeView that fills the top part of the application, but since the number of items in the TreeView changes, my Apply button changes its position vertically. Is there a way to anchor it to the bottom part of the window, so it's always on the right bottom part of the window, i.e. 10x10 distance from the right bottom edge.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤蝉 2024-10-27 15:13:01

要实现锚定,请设置以下属性:

  • Width="auto"
  • Height="auto"
  • VerticalAlignment="Stretch"
  • Horizo​​ntalAlignment="Stretch"
  • 根据需要设置边距。

例子:

    <TabControl

        Name="tabControl1"

        HorizontalAlignment="Stretch" 

        Margin="40,40,40,40"  

        Width="auto" 

        Height="auto" 

        VerticalAlignment="Stretch">

To achieve anchoring, Set following properties:

  • Width="auto"
  • Height="auto"
  • VerticalAlignment="Stretch"
  • HorizontalAlignment="Stretch"
  • Set margins as needed.

Example:

    <TabControl

        Name="tabControl1"

        HorizontalAlignment="Stretch" 

        Margin="40,40,40,40"  

        Width="auto" 

        Height="auto" 

        VerticalAlignment="Stretch">
空城缀染半城烟沙 2024-10-27 15:13:01

您可以在 TreeView 或 Button 上设置 Horizo​​ntalAlignment 和 VerticalAlignment (我不知道您要锚定哪个控件),也可以将控件放置在 DockPanel 内。

要将控件与其他控件分开,请使用边距。

Margin="0 0 10 10"

只会给你右边和底部的边距

我希望这能让它更清楚一点:

<Window>   
  <DockPanel VerticalAlignment="Stretch">
    <TreeView DockPanel.Dock="Top" />
    <Button DockPanel.Dock="Bottom" Margin="0 0 10 10" />   
  </DockPanel>
</Window>

You can either set the HorizontalAlignment and VerticalAlignment on the TreeView or Button (I cannot tell which control you are trying to anchor), or you could place the control inside a DockPanel.

To space the control away from others use the Margin.

Margin="0 0 10 10"

will give you the margin only on the right and bottom

I hope this makes it a bit clearer:

<Window>   
  <DockPanel VerticalAlignment="Stretch">
    <TreeView DockPanel.Dock="Top" />
    <Button DockPanel.Dock="Bottom" Margin="0 0 10 10" />   
  </DockPanel>
</Window>
执笏见 2024-10-27 15:13:01

我认为您在按钮的 XAML 中需要这个:

HorizontalAlignment="Right" VerticalAlignment="Bottom"

I think you need this in the XAML of your button:

HorizontalAlignment="Right" VerticalAlignment="Bottom"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文