我有一个画布,我正在填充代码(C#),我正在绘制的是基于参数的建筑物的动态模型,所以我需要它来缩放以适应画布区域100%即使它超过了画布宽度和高度。我能找到的唯一解决方案是使用画布元素并将它们拉伸以适应,我需要缩小它们以适应。
这是我最接近的:
<Grid>
<Viewbox Stretch="Uniform" HorizontalAlignment="Left" VerticalAlignment="Top">
<Canvas x:Name="Canvas" Background="Transparent" Margin="0" Width="800" HorizontalAlignment="Left" VerticalAlignment="Top" Height="600" >
<Canvas.LayoutTransform>
<ScaleTransform ScaleX="1" ScaleY="1" />
</Canvas.LayoutTransform>
</Canvas>
</Viewbox>
</Grid>
但这并不能完美地扩展,但它是我最接近的。对此最好的解决方案是什么?
编辑:代码背后:
var areaRectangle = new Rectangle();
areaRectangle.Stroke = Brushes.Red;
areaRectangle.StrokeThickness = 3;
var areaTextblock = new TextBlock();
areaTextblock.Text = child.Name;
areaTextblock.Margin = new Thickness(48, 6, 0, 0);
areaRectangle.Width = (childData.Length.GetValueOrDefault() - 6);
areaRectangle.Height = (childData.Width.GetValueOrDefault() - 6);
Canvas.SetLeft(areaRectangle, areaLeft + 3);
Canvas.SetTop(areaRectangle, 3);
Canvas.SetLeft(areaTextblock, areaLeft + 3);
Canvas.SetTop(areaTextblock, 3);
Canvas.Children.Add(areaRectangle);
Canvas.Children.Add(areaTextblock);