如何在xaml WPF中设置stackpanel或grid上的绝对位置

时间:2022-11-18 17:35:15

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使用绝对位置。

2 个解决方案

#1


8  

You have to use Canvas in 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 :

输出是:

如何在xaml WPF中设置stackpanel或grid上的绝对位置

Feel free to ask if help is needed.

随意询问是否需要帮助。

#2


2  

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. 根网格下的元素

  3. Elements in a canvas that is the same size as the window (as Vasilievski pointed out)
  4. 画布中与窗口大小相同的元素(如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>

#1


8  

You have to use Canvas in 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 :

输出是:

如何在xaml WPF中设置stackpanel或grid上的绝对位置

Feel free to ask if help is needed.

随意询问是否需要帮助。

#2


2  

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. 根网格下的元素

  3. Elements in a canvas that is the same size as the window (as Vasilievski pointed out)
  4. 画布中与窗口大小相同的元素(如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>