1

我在一个显示在 ChildWindow 中的网格中有一个边框。我需要使边框更大,所以我对其应用了 RenderTransform。但是 ChildWindow 没有扩展以适应缩放的边框。看起来在应用渲染转换之前正在计算大小。这意味着边界现在被裁剪了,我只能看到它的 1/4。

我尝试将边框包装在 ScrollViewer 和 ViewBox 中,但都没有奏效。

<Grid>
    ...
    <Border x:Name="Border"
            Grid.Row="3"
            Grid.ColumnSpan="2"
            Background="White"
            BorderBrush="Black"
            BorderThickness="1"
            CornerRadius="5"
            VerticalAlignment="Top"
            Height="{Binding NewLabelTemplate.Height}"
            Width="{Binding NewLabelTemplate.Width}">
        <Border.RenderTransform>
            <ScaleTransform CenterX="0.5"
                            CenterY="0.5"
                            ScaleX="2"
                            ScaleY="2"/>
        </Border.RenderTransform>
        ...
    </Border>
    ...
</Grid>

在计算其大小时,如何让 ChildWindow 使用边框的最终大小?

我不能将比例应用于 ChildWindow 或直接使用边框的渲染高度和宽度,因为 Grid 中有其他我不想缩放的元素。

4

1 回答 1

1

目前我能想到两种可能的解决方案:

1. 计算要绑定的边框的缩放属性

<Grid>
    <Grid.Resources>
        <ScaledSizeProperties x:Key="BorderSizes"
            ScaleFactor="2"
            BorderThickness="1"
            CornerRadius="5"
            Height="{Binding NewLabelTemplate.Height}"
            Width="{Binding NewLabelTemplate.Width}"/>
    </Grid.Resources>
    ...
    <Border x:Name="Border"
        Grid.Row="3"
        Grid.ColumnSpan="2"
        Background="White"
        BorderBrush="Black"
        VerticalAlignment="Top"
        BorderThickness="{Binding
            Path=ScaledBorderThickness, Source={StaticResource BorderSizes}}"
        CornerRadius="{Binding
            Path=ScaledCornerRadius, Source={StaticResource BorderSizes}}"
        Height="{Binding
            Path=ScaledHeight, Source={StaticResource BorderSizes}}"
        Width="{Binding
            Path=ScaledWidth, Source={StaticResource BorderSizes}}">
        ...
    </Border>
    ...
</Grid>

和代码:

public class ScaledSizeProperties : DependencyObject
{
    //add input DependencyProperties for:
    //double ScaleFactor
    //double BorderThickness
    //double CornerRadius
    //double Height
    //double Width

    //add output DependencyProperties for:
    //double ScaledBorderThickness
    //double ScaledCornerRadius
    //double ScaledHeight
    //double ScaledWidth

    public double ScaleFactor
    {
        get { return (double) GetValue( ScaleFactorProperty ); }
        set { SetValue( ScaleFactorProperty, value ); }
    }

    public static readonly DependencyProperty ScaleFactorProperty =
        DependencyProperty.Register( "ScaleFactor", typeof( double ),
        typeof( ScaledSizeProperties ),
        new PropertyMetadata( 1, OnScaleFactorChanged ) );

    private static void OnScaleFactorChanged(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        //recalculate all size properties
    }

    public double Height
    {
        get ...
        set ...
    }

    ... DependencyProperty HeightProperty ... OnHeightChanged ...

    private static void OnHeightChanged(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        //recalculate ScaledHeight
    }
}

2. 使用 LayoutTransformer 而不是 RenderTransform

作为 Toolkit 一部分的LayoutTransformer现在(自 SL5 起)是 Silverlight 基础库包的正式部分。

<Grid>
...
<LayoutTransformer
    Grid.Row="3"
    Grid.ColumnSpan="2"
    VerticalAlignment="Top">
    <LayoutTransformer.LayoutTransform>
        <ScaleTransform CenterX="0.5"
                        CenterY="0.5"
                        ScaleX="2"
                        ScaleY="2"/>
    </LayoutTransformer.LayoutTransform>
    <Border x:Name="Border"
        Background="White"
        BorderBrush="Black"
        BorderThickness="1"
        CornerRadius="5"
        Height="{Binding NewLabelTemplate.Height}"
        Width="{Binding NewLabelTemplate.Width}">
        ...
    </Border>
</LayoutTransformer>
...
</Grid>
于 2014-10-06T08:54:32.517 回答