Search code examples
wpfwpf-controlstextblock

WPF Textblock translation based on reference point


Please check the attached image. I made the textblock origin to it's center by RenderTransformOrigin="0.5,0.5". Now I would like to move the textblock based on the reference point. May be (0,0 ) or (10,10) or ...... These are absolute points. For example, in case of (0,0) the textblock should move to absolute (0,0) based on its reference point. I know how to move relatively by TranslateTransform, but this case I need absolute transform.

enter image description here


Solution

  • Maybe you need this

    void MoveToPoint(UIElement sender, Point point)
    {
        Canvas.SetLeft(sender, point.X - sender.RenderTransformOrigin.X * sender.ActualWidth);
        Canvas.SetTop(sender, point.Y - sender.RenderTransformOrigin.Y * sender.ActualHeight);
    }
    

    to Xaml (patern MVVM): you need write converter and for properties Canvas.Left and Canvas.Top use Binding. It's more code than previously.

    Example Converter (example 6) http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part

    In converter need send 2 parameters sender.RenderTransformOrigin.X and ActualWidth.

    Watch Converter with some params (in russian) - http://habrahabr.ru/post/141107/