【问题标题】:Limit ScaleTransform to a maximum height and width将 ScaleTransform 限制为最大高度和宽度
【发布时间】:2017-12-08 15:41:46
【问题描述】:

有没有办法将ScaleTransform 限制为最大高度和宽度?

我有一个Viewbox,背景为Image,设置为“无”。在这个Viewbox 中,我还有一个ItemsControlCanvas 上呈现较小的图像。由于背景图片可以由操作员选择,因此图像的像素大小会有所不同,因此操作员需要能够相应地缩放较小的图像。

但是,如果在中等大小的背景图像上将缩放因子设置得非常大,或者背景图像的尺寸很小,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


【解决方案1】:

根据Clemens' 的评论,在代码中做限制工作正常。我刚刚计算了滑块的最大缩放值,基于最大的较小图像(推进器)被缩放到背景图像宽度的一半。这最终成为了我的解决方案。

C#:

private string _backgroundImageFilePath;
public string BackgroundImageFilePath {
    get { return _backgroundImageFilePath; }
    set {
        SetNotify(ref _backgroundImageFilePath, value);
        try {
            BackgroundImage = new BitmapImage(new Uri(BackgroundImageFilePath));
        }
        catch (FileNotFoundException) {
            BackgroundImage = ImageNotFoundBitmapImage;
        }
        catch (Exception) {
            BackgroundImage = ImageInvalidBitmapImage;
        }
        ThrusterImageMaximumScale = BackgroundImage.Width / (2 * RovIllustrationThruster.VerticalThrusterBitmapImage.Width);
        ThrusterImageMaximumScale = Math.Round(ThrusterImageMaximumScale, 1);
        if (ThrusterImageScale > ThrusterImageMaximumScale) {
            ThrusterImageScale = ThrusterImageMaximumScale;
        }
        ConfigurationHandler.IsDirty = true;
    }
}

【讨论】:

    猜你喜欢
    • 2013-09-23
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2014-02-27
    • 2013-05-23
    • 1970-01-01
    相关资源
    最近更新 更多