如何在 xaml WPF 中的堆栈面板或网格上设置绝对位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35064843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:38:59  来源:igfitidea点击:

How can set absolute position on stackpanel or grid in xaml WPF

c#csswpfxamlposition

提问by somePeaple

Is it possible to set my StackPanel or Grid to be position absolute like CSS. In CSS is have property Position of the elements and can set to be relative, absolute and is working good.

是否可以将我的 StackPanel 或 Grid 设置为像 CSS 一样的绝对位置。在 CSS 中具有元素的属性 Position 并且可以设置为相对、绝对并且运行良好。

In XAML can make Grid, StackPanel to use position absolute.

在 XAML 中可以使 Grid、StackPanel 使用绝对位置。

回答by Vasilievski

You have to use Canvasin order to set absolute position in WPF.

您必须使用Canvas才能在 WPF 中设置绝对位置。

In case of buttons in a window, here is a sample :

对于窗口中的按钮,这是一个示例:

<Window x:Class="tobedeleted.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
     <Canvas>
        <Button Canvas.Left="10" Canvas.Bottom="20">Bottom left</Button>
    </Canvas>
</Window>

The output is :

输出是:

enter image description here

在此处输入图片说明

Feel free to ask if help is needed.

随时询问是否需要帮助。

回答by Geoffrey

Absolute positioning defeats the purpose of WPF, but I agree, sometimes there is no other way so you have two basic options.

绝对定位违背了 WPF 的目的,但我同意,有时没有其他方法,因此您有两个基本选择。

  1. Elements under the root grid
  2. Elements in a canvas that is the same size as the window (as Vasilievski pointed out)
  1. 根网格下的元素
  2. 画布中与窗口大小相同的元素(如 Vasilievski 指出的)

Code example:

代码示例:

<Window x:Class="WpfApplication1.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Window3" Height="300" Width="300">
    <Grid>

        <Rectangle Fill="Red" Width="100" Height="120"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   Panel.ZIndex="13"
                   Margin="12,34"
                   />
        <Rectangle Fill="Green" Width="100" Height="120"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   Margin="24,54"
                   />

        <Canvas>
            <Rectangle Canvas.Left="5" Canvas.Top="5" Panel.ZIndex="2" Fill="Yellow" Width="120" Height="30" />
            <Rectangle Canvas.Left="25" Canvas.Top="17" Panel.ZIndex="0" Fill="Blue" Width="120" Height="30" />
        </Canvas>

    </Grid>
</Window>