WPF:如何使(0,0)在一个画布的中心 [英] WPF: how to make the (0,0) in center inside a Canvas

查看:326
本文介绍了WPF:如何使(0,0)在一个画布的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF Canvas具有从控件左上角(0,0)开始的坐标系。

The WPF Canvas has a coordinate system starting at (0,0) at the top-left of the control.

例如,设置以下内容将使我的控件出现在左上角:

For example, setting the following will make my control appear on the top-left:

<Control Canvas.Left="0" Canvas.Top="0">

如何将其更改为标准笛卡尔坐标

How can I change it to the standard cartesian coordinates?

基本上:


  • (0,0)
  • 翻页Y

我注意到了这个帖子是类似的,但它不谈论平移坐标系统。我尝试添加一个TranslateTransform,但我不能让它工作。

I noticed this post is similar, but it does not talk about translating the coordinate system. I tried adding a TranslateTransform, but I can't make it work.

推荐答案

最好的事情是写一个自定义Canvas,在其中您可以写入ArrangeOverride,使得它以0,0为中心。

The best thing is to write a custom Canvas, in which you can write ArrangeOverride in such a way that it takes 0,0 as the center.

更新:我在下面的答案中给了另一个评论(@decasteljau)我不建议从Canvas派生,你可以从面板派生,并添加两个附加的依赖属性顶部和左边,并把上面粘贴的相同的代码。也不需要一个LayoutTransform的构造函数,不要使用任何变换的面板代码使用适当的测量和排列基于面板的DesiredSize这样你就可以得到漂亮的内容调整行为太。当画布大小更改时,Canvas不会动态定位项目。

Update : I had given another comment in the below answer (@decasteljau) I wont recommend deriving from Canvas, You can derive from Panel and add two Attached Dependancy properties Top and Left and put the same code you pasted above. Also doesnt need a constructor with LayoutTransform in it, And dont use any transform on the panel code use proper measure and arrange based on the DesiredSize of the panel So that you can get nice content resize behavior too. Canvas doesn't dynamically position items when the Canvas size changes.

这篇关于WPF:如何使(0,0)在一个画布的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆