【发布时间】:2017-12-08 15:41:46
【问题描述】:
有没有办法将ScaleTransform 限制为最大高度和宽度?
我有一个Viewbox,背景为Image,设置为“无”。在这个Viewbox 中,我还有一个ItemsControl 在Canvas 上呈现较小的图像。由于背景图片可以由操作员选择,因此图像的像素大小会有所不同,因此操作员需要能够相应地缩放较小的图像。
但是,如果在中等大小的背景图像上将缩放因子设置得非常大,或者背景图像的尺寸很小,ItemsControl 图像最终会溢出容器并阻塞其他功能。
那么如何设置ScaleTransform 的限制?
XAML:
<Viewbox Grid.Column="1">
<Grid>
<Image Source="{Binding Path=BackgroundImage}" Stretch="None" />
<ItemsControl ItemsSource="{Binding Path=ThrusterCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.Bottom" Value="{Binding Path=CanvasYPosition}" />
<Setter Property="Canvas.Left" Value="{Binding Path=CanvasXPosition}" />
<Setter Property="LayoutTransform">
<Setter.Value>
<ScaleTransform ScaleX="{Binding Path=ParentRovIllustration.ThrusterImageScale}"
ScaleY="{Binding Path=ParentRovIllustration.ThrusterImageScale}" />
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="{Binding Path=ThrusterImage}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Viewbox>
截图:
【问题讨论】:
-
ScaleTransform 对它所应用的元素或属性一无所知,因此不能单独限制宽度或高度。对 ThrusterImageScale 属性实施适当的限制。
-
感谢您的提示。它就像一个魅力。
标签: c# wpf image scaletransform