带滑块绑定的TranslateTransform不起作用

时间:2013-09-23 07:16:25

标签: c# wpf canvas slider

无法理解为什么Translatetransform绑定不起作用。我有3个滑块 - 一个用于缩放,2个用于x和y变换(我在代码中将圆圈作为Canvas的子元素)。

我在XAML下面使用:

<Window x:Class="Visualizer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" WindowState="Maximized" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
    <Canvas HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Name="MainCanvas" >
        <Canvas HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Name="OperatorsCanvas" >
            <ContentControl x:Name="OperatorsContent"/>
            <Canvas.LayoutTransform>
                <TransformGroup>
                    <ScaleTransform 
                    CenterX="0" 
                    CenterY="0"
                    ScaleX="{Binding ElementName=ScaleSlider,Path=Value}"
                    ScaleY="{Binding ElementName=ScaleSlider,Path=Value}"
                    />
                    <TranslateTransform X="{Binding ElementName=TranslateXSlider,Path=Value}" Y="{Binding ElementName=TranslateYSlider,Path=Value}" />
                </TransformGroup>
            </Canvas.LayoutTransform>
        </Canvas>
        <Slider Canvas.Right="5" Width="154" Canvas.Top="5" HorizontalAlignment="Right" Name="ScaleSlider" ToolTip="Determines the UI scale factor."
                    Value="1" Minimum="0.1" Maximum="4"/>
        <Slider Canvas.Bottom="0" Width="762"  HorizontalAlignment="Stretch" Name="TranslateXSlider" Value="0" Minimum="-20" Maximum="20"/>
        <Slider Canvas.Right="0" Canvas.Top="282" Width="423" RenderTransformOrigin="0.494,0.455" Height="16" Name="TranslateYSlider"  Value="0" Minimum="-20" Maximum="20" >
            <Slider.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Slider.RenderTransform>
        </Slider>
    </Canvas>
</Window>

缩放有效,但变换不起作用。

1 个答案:

答案 0 :(得分:5)

LayoutTransform中的翻译将被忽略,如MSDN上LayoutTransform文档的“备注”部分所述。您必须将LayoutTransform替换为RenderTransform才能使其正常运行:

<Canvas ...>
    <Canvas.RenderTransform>
        <TransformGroup>
            <ScaleTransform
                ScaleX="{Binding ElementName=ScaleSlider,Path=Value}"
                ScaleY="{Binding ElementName=ScaleSlider,Path=Value}"/>
            <TranslateTransform
                X="{Binding ElementName=TranslateXSlider,Path=Value}"
                Y="{Binding ElementName=TranslateYSlider,Path=Value}"/>
        </TransformGroup>
    </Canvas.RenderTransform>
    ...
</Canvas>

另请注意,Slider具有Orientation属性。因此,没有必要在那里应用转换。