如何将画布的x,y原点更改为中心,WPF

如何将画布的x,y原点更改为中心,WPF,wpf,canvas,wpf-controls,Wpf,Canvas,Wpf Controls,伙计们,我正在使用画布作为ItemsPanelTemplate,并将其绑定到包含典型行起点和终点的行列表 <ItemsControl ItemsSource="{Binding Path = LineList}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas/> </ItemsPanelTemplate>

伙计们,我正在使用画布作为ItemsPanelTemplate,并将其绑定到包含典型行起点和终点的行列表

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

现在。我想把中心点移到画布的中间,而不是左上角。我面前几乎没有选择

  • 使用值转换器并根据画布大小和显示调整值[调整x和y值]
  • 转换画布,如以下文章中所述:和
  • 我知道如何使用第一种方法,但当我尝试使用第二种方法时,它并没有改变坐标系。为什么呢?我只是在我的代码中替换了答案,如下所示。我错过什么了吗

    ****更新****:以下代码正常工作

    <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>
    

    示例中ScaleTransform的
    CenterX
    CenterY
    属性实际上不会移动画布。此外,LayoutTransform忽略了翻译(参见备注)。您可以为画布使用适当的
    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>
    

    示例中ScaleTransform的
    CenterX
    CenterY
    属性实际上不会移动画布。此外,LayoutTransform忽略了翻译(参见备注)。您可以为画布使用适当的
    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>
    
    
    
    代码对您有用吗?我只是替换了你的代码而不是我的代码,似乎没有响应,结果和以前一样。为什么?是的,我已经测试过了,效果很好。当然,ItemsControl必须放置在适当的外部面板中,该面板进行必要的布局,例如网格。画布是不行的。好吧,这是我的错误,你的画布行得通,但如何用你的画布反转Y轴。因为,在我们的例子中,Y是自上而下测量的,只需更改第一个ScaleTransform的Y符号:
    ScaleY=“-2”
    。代码是否适合您?我只是替换了你的代码而不是我的代码,似乎没有响应,结果和以前一样。为什么?是的,我已经测试过了,效果很好。当然,ItemsControl必须放置在适当的外部面板中,该面板进行必要的布局,例如网格。画布是不行的。好吧,这是我的错误,你的画布行得通,但如何用你的画布反转Y轴。因为在我们的例子中,Y是从上到下测量的,只需更改第一个ScaleTransform的Y符号:
    ScaleY=“-2”