How can set absolute position on stackpanel or grid in xaml WPF
23,631
Solution 1
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 :
Feel free to ask if help is needed.
Solution 2
Absolute positioning defeats the purpose of WPF, but I agree, sometimes there is no other way so you have two basic options.
- Elements under the root grid
- Elements in a canvas that is the same size as the window (as Vasilievski pointed out)
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>
Author by
somePeaple
Updated on January 29, 2020Comments
-
somePeaple about 4 years
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.
-
somePeaple about 8 yearsHow can I set in canvas to have absolute position in the window?
-
Vasilievski about 8 yearsAnswer edited, don't forget to mark my answer as checked if it is good for you.
-
somePeaple about 8 yearsIs good, thanks for the answer. I'm not forget just thinking is possible to make may problem, because this is one little part of the problem.
-
somePeaple about 8 yearsAnd you don't forget if you think the question is good to check is useful and clear
-
Ryan about 6 yearsAbsolute positioning doesn't defeat the purpose of WPF, I think you're confused about the term.
-
StayOnTarget about 5 years@Ryan I agree particularly graphically rich scenarios
-
Paul McCarthy over 4 yearsYes, Absolute positioning is the way to go. Finally I can put the controls where I want, rather that having them jump around to stupid places. See also this question for how to set in the code behind. stackoverflow.com/questions/21515135/…