Search code examples
c#csswpfxamlposition

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


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.

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


Solution

  • You have to use Canvas in order to set absolute position in 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.