我想在Canvas中安装Image并将其安装到窗口中。当画布为空时它工作正常(画布在窗口中调整大小),但是当我在其中添加图像时,画布不再适合窗口,即使我有Stretch =" Uniform"应用于图像。我在下面说明了这种行为。遗憾的是,使用画布是因为我在它上面绘制形状。有什么想法吗?
不可
<Window x:Class="ImageCropper.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300">
<Border BorderThickness="3" BorderBrush="Red">
<Canvas Background="Blue">
</Canvas>
</Border>
</Window>
为
<Window x:Class="ImageCropper.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300">
<Border BorderThickness="3" BorderBrush="Red">
<Canvas Background="Blue">
<Image Source="asd.png" Stretch="Uniform" />
</Canvas>
</Border>
</Window>
答案 0 :(得分:12)
我不认为拉伸画布非常好。看到这个答案: https://stackoverflow.com/a/6010270/93233
然而,我能够使用以下内容:
<Border BorderThickness="3" BorderBrush="Red">
<Canvas Background="Blue" Name="canvas1">
<Image Source="asd.png" Width="{Binding Path=ActualWidth, ElementName=canvas1}" Height="{Binding Path=ActualHeight, ElementName=canvas1}" Stretch="Uniform"/>
</Canvas>
</Border>