How to center an element in wpf canvas
Asked Answered
C

4

33

How can I center an element in wpf canvas using attached properties?

Chatman answered 5/2, 2010 at 17:16 Comment(0)
R
29

Something like this.

double left = (Canvas.ActualWidth - element.ActualWidth) / 2;
Canvas.SetLeft(element, left);

double top  = (Canvas.ActualHeight - element.ActualHeight) / 2;
Canvas.SetTop(element, top);
Restive answered 5/2, 2010 at 17:24 Comment(1)
can this be written inside a class which inherits a canvas class as an attached property.Chatman
C
48

I came across this post, because I was searching for a way to center an element within a Canvas in XAML only, instead of using Attached Properties.

Just in case, you came for the same reason:

<Canvas x:Name="myCanvas">
    <Grid Width="{Binding ActualWidth, ElementName=myCanvas}" 
          Height="{Binding ActualHeight, ElementName=myCanvas}">
        <Label Content="Hello World!"
               HorizontalAlignment="Center"
               VerticalAlignment="Center" 
        />
    </Grid>
</Canvas>

One more thing, though

Beware that every solution here is a trap, depending on what you try to achieve. Just because your element is within the Canvas on a hierarchical order, it will not be part of the Canvas itself. A Canvas is something to draw on and not to place controls in. So you cannot use the Canvas API for those controls, because the Grid is like an isolated overlay, obviously.

If you want a separated overlay, whilst the Canvas is your background, you are good to go with this solution.

Corazoncorban answered 9/3, 2016 at 17:27 Comment(4)
Awesome! This works "as is", with no code behind. I used it to center a UserControl, in the place of the Label.Brunel
Now there is a slow Grid inside. A solution without a Grid would be nice.Interlanguage
@MathiasMüller There's nothing to stop you using any other control in place of the Grid.Zeeland
Save my day. I did not though to add a grid to handle the layout. Thank you @MartinBraunBowerman
R
29

Something like this.

double left = (Canvas.ActualWidth - element.ActualWidth) / 2;
Canvas.SetLeft(element, left);

double top  = (Canvas.ActualHeight - element.ActualHeight) / 2;
Canvas.SetTop(element, top);
Restive answered 5/2, 2010 at 17:24 Comment(1)
can this be written inside a class which inherits a canvas class as an attached property.Chatman
T
10

The only way I know to do this is to figure out the size of the canvas, and then set the properties based off that. This can be done using an event handler for SizeChanged on the canvas:

parentCanvas.SizeChanged += new SizeChangedEventHandler(parentCanvas_SizeChanged);

void parentCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
    parentCanvas.SetLeft(uiElement, (parentCanvas.ActualWidth - uiElement.ActualWidth) / 2);
    parentCanvas.SetTop(uiElement, (parentCanvas.ActualHeight - uiElement.ActualHeight) / 2);
}
Tacky answered 5/2, 2010 at 17:30 Comment(3)
will this event fire on load of the window.Chatman
It should, yes. It will fire whenever the ActualWidth/ActualHeight properties of the canvas are changed. (i.e. during layout updating)Tacky
Great. But I believe it should be Canvas.SetLeft and Canvas.SetTop; not the instance of the parent Canvas.Projector
S
3

You can put the Canvas and the element you want to be centered inside a Grid :

<Grid>
    <Canvas Width="200" Height="200" Background="Black" />
    <Button Width="50" Height="20" > Hello
    </Button>
</Grid>
Storehouse answered 22/11, 2013 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.