Whilst work­ing on an image view I needed the abil­ity to bind the value of a Slider to a Scale­Trans­form as a per­cent­age value and at the same time dis­play the value of the slider in a label. So for a zoom range from 1% to 250% you would usu­ally set the Min­i­mum and Max­i­mum prop­erty of your Slider to 0.01 and 2.5 respec­tively, by then you wouldn’t be able to dis­play the zoom value as a per­cent­age, e.g. Zoom 148%.

So to get around this lim­i­ta­tion, in the end I used the range 1 to 250 for the Slider and added another Scale­Trans­form with ScaleX and Sca­leY set to 0.01, like this:

<StackPanel x:Name="LayoutRoot" Background="Gray">
    <StackPanel Orientation="Horizontal">
        <TextBlock Margin="5,5,10,5">Zoom</TextBlock>
        <TextBlock Margin="5,5,0,5" Text="{Binding Path=Value, ElementName=sldZoom, Mode=OneWay}"/>
        <TextBlock Margin="0,5">%</TextBlock>
    </StackPanel>

    <Slider x:Name="sldZoom" Orientation="Horizontal" Minimum="1" Maximum="250" Value="100"/>

    <Image Source="bingonet.png" RenderTransformOrigin="0, 0">
        <Image.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleX="0.01" ScaleY="0.01"/>
                <ScaleTransform>
                    <ScaleTransform.ScaleX>
                        <Binding ElementName="sldZoom" Path="Value" Mode="OneWay"/>
                    </ScaleTransform.ScaleX>
                    <ScaleTransform.ScaleY>
                        <Binding ElementName="sldZoom" Path="Value" Mode="OneWay"/>
                    </ScaleTransform.ScaleY>
                </ScaleTransform>
            </TransformGroup>
        </Image.RenderTransform>
    </Image>
</StackPanel>
Share

5 Responses to “WPF — how to bind ScaleTransform to Slider, percentage style!”

  1. Gali says:

    Thanks! This one helped me alot.
    Just needed to change the ini­tial ScaleX and Y to 0.001

  2. Slate says:

    Alter­na­tivly you can use String for­mat­ting to prop­erly con­vert to a percentage.

    Zoom

  3. Ken says:

    Im using RenderTransformOrigin=“0.5, 0.5″ to scale from the cen­ter of the image. This works except the image grows in size and cov­ers all my other con­trols. Do I need to clip the image or is there an eas­ier way?

  4. Ken says:

    Fixed my issue by adding a bor­der with ClipToBounds=“Yes” around the image.

  5. kapil says:

    Awe­some thank u

Leave a Reply