2014-04-04 39 views
0

,大家好我用的帆布作为ItemsPanelTemplate并将其绑定到包含典型的线路起点和终点如何改变X,画布中心y原点,WPF

<ItemsControl ItemsSource="{Binding Path = LineList}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

现在行列表。我想将中心点移到画布中间而不是左上角。在我面前的选项很少

  1. 使用值转换器并根据画布大小调整值并显示[调整x和y的值]
  2. 改造画布提以下职位:How to change x,y origin of canvas...?Ray's answer

我知道如何通过第一种方法去做,但是当我通过第二种方法试了一下是不会改变的坐标系。这是为什么?我只是用我的代码替换了下面的答案。我错过了什么吗?

**** ****更新:下面的代码工作正常

<ItemsControl ItemsSource="{Binding Path = LineList}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas> 
      <Canvas.LayoutTransform> 
       <ScaleTransform ScaleX="1" ScaleY="-1" CenterX=".5" CenterY=".5" /> 
      </Canvas.LayoutTransform> 
      </Canvas> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

回答

5

在您的示例ScaleTransform的CenterXCenterY性质没有发生改变的画布。此外,翻译无论如何都会被LayoutTransform忽略(请参阅备注here)。您可以改为使用适当的RenderTransform作为画布:

<ItemsPanelTemplate> 
    <Canvas> 
     <Canvas.RenderTransform> 
      <TransformGroup> 
       <ScaleTransform ScaleX="2" ScaleY="2"/> 
       <TranslateTransform 
        X="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Canvas}}" 
        Y="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType=Canvas}}"/> 
       <ScaleTransform ScaleX="0.5" ScaleY="0.5"/> 
      </TransformGroup> 
     </Canvas.RenderTransform> 
    </Canvas> 
</ItemsPanelTemplate> 
+0

该代码是否适合您?我只是用你的代码代替了我的代码,看起来没有反应,结果和以前一样。这是为什么? – RobinAtTech

+0

是的,我测试过了,效果很好。当然,ItemsControls必须放在一个适当的外部面板上,以进行必要的布局,例如,一个网格。画布不会为此工作。 – Clemens

+0

嗯,这是我的错误,你的工作,但如何反转与你的一个Y轴。因为在我们的例子中Y是从上到下测量的 – RobinAtTech