问题描述
请检查附加的图像.我通过 RenderTransformOrigin="0.5,0.5" 将文本块原点设为其中心.现在我想根据参考点移动文本块.可能是 (0,0 ) 或 (10,10) 或......这些是绝对点.例如,在 (0,0) 的情况下,文本块应根据其参考点移动到绝对 (0,0).我知道如何通过 TranslateTransform 进行相对移动,但这种情况下我需要绝对转换.
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.
推荐答案
也许你需要这个
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);
}
到 Xaml(模式 MVVM):您需要编写转换器,并且对于属性 Canvas.Left 和 Canvas.Top 使用绑定.代码比以前多.
to Xaml (patern MVVM):you need write converter and for properties Canvas.Left and Canvas.Top use Binding. It's more code than previously.
示例转换器(示例 6)http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part
Example Converter (example 6) http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part
在转换器中需要发送 2 个参数 sender.RenderTransformOrigin.X 和 ActualWidth.
In converter need send 2 parameters sender.RenderTransformOrigin.X and ActualWidth.
带有一些参数的手表转换器(俄语) - http://habrahabr.ru/post/141107/
Watch Converter with some params (in russian) - http://habrahabr.ru/post/141107/
这篇关于基于参考点的 WPF Textblock 翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!