Visual.AnchorPoint Visual.AnchorPoint Visual.AnchorPoint Visual.AnchorPoint Property

Definition

The point on the visual to be positioned at the visual's offset. Value is normalized with respect to the size of the visual. Animatable.

public : Vector2 AnchorPoint { get; set; }
Vector2 AnchorPoint();

void AnchorPoint(Vector2 anchorpoint);
public Vector2 AnchorPoint { get; set; }
Public ReadWrite Property AnchorPoint As Vector2

Property Value

Vector2 Vector2

The point on the visual to be positioned at the visual's offset. Value is normalized with respect to the size of the visual.

Additional features and requirements

Device family
Windows 10 (introduced v10.0.10586.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v2)

Examples


using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Composition;
using Windows.UI.Xaml.Hosting;
using System.Numerics;
 
namespace AnchorPointSample
{
    /// <summary>
    /// Using AnchorPoint to Center a Rectangle
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }
 
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Get the backing visual for the "CenteredElement" XAML Element
            Visual root = ElementCompositionPreview.GetElementVisual(CenteredElement);
            Compositor compositor = root.Compositor;
 
            // Create a solid color visual
            SpriteVisual mainVisual = compositor.CreateSpriteVisual();
            mainVisual.Size = new Vector2(200, 200);
            mainVisual.Brush = compositor.CreateColorBrush(Windows.UI.Colors.Blue);
 
            // Set the anchor point to center the rectangle
            mainVisual.AnchorPoint = new Vector2(0.5f, 0.5f);
 
            // Insert the rectangle as a child of the XAML element
            ElementCompositionPreview.SetElementChildVisual(CenteredElement, mainVisual);
        }
    }
}