Wpf 画布尺寸过大时会被剪裁

Wpf 画布尺寸过大时会被剪裁,wpf,canvas,Wpf,Canvas,我使用带有ImageBrush的画布显示图像。我正在将画布的大小设置为图像的原始大小,以便在移动鼠标等时获得坐标 问题是,当我将画布放入一个较小尺寸的控件(Grid)时,canvas会被剪裁 <Grid> <Canvas Width="{Binding ImageWidth}" Height="{Binding ImageHeight}" > <Canvas.Background> <ImageBrush

我使用带有
ImageBrush
的画布显示图像。我正在将画布的大小设置为图像的原始大小,以便在移动鼠标等时获得坐标

问题是,当我将画布放入一个较小尺寸的控件(
Grid
)时,
canvas
会被剪裁

  <Grid>
    <Canvas Width="{Binding ImageWidth}" Height="{Binding ImageHeight}" >
        <Canvas.Background>
          <ImageBrush ImageSource="{Binding Image, Converter={StaticResource imgConverter}}"/>
        </Canvas.Background>
    </Canvas>
  </Grid>


有没有一种方法可以保持画布的大小而不被剪裁?

我一直想深入到源代码中,找出剪裁发生的位置,但从来没有花时间去做。我一直在使用一种不太好的技巧,将
画布
插入到可视化树中,作为一种解决方法

有许多控件剪辑孩子的视觉效果
Grid
StackPanel
,等等。正如我提到的,通常的快速修复方法是在导致剪辑的容器之后使用
画布

在您的代码片段中,是否有更多的容器位于可视化树的更高位置

如果深度实际上是这样的:-

<StackPanel>
    <Grid>
        <Canvas Width="{Binding ImageWidth}" Height="{Binding ImageHeight}" >
            <Canvas.Background>
              <ImageBrush ImageSource="{Binding Image, Converter={StaticResource imgConverter}}"/>
            </Canvas.Background>
        </Canvas>
    </Grid>
</StackPanel>

如果改变了其他控件的其他布局需求,此解决方案可能会出现问题。

将画布放在一个视图框中,使其缩小以适应网格大小,或者使用ScrollViewer使画布可滚动。
<StackPanel>
    <Canvas>
        <Grid>
            <Canvas Width="{Binding ImageWidth}" Height="{Binding ImageHeight}" >
                <Canvas.Background>
                  <ImageBrush ImageSource="{Binding Image, Converter={StaticResource imgConverter}}"/>
                </Canvas.Background>
            </Canvas>
        </Grid>
    </Canvas>
</StackPanel>