简体   繁体   中英

Rotate image when moving slider

I want to rotate an image according to the slider movement.

Please find the below image.

在此处输入图片说明

The four ticks in the slider(slider1) are 0, 30, 60 and 90 degrees respectively. I want to rotate the image(image1) according to the angle chosen in the slider.

I could rotate the image on a button using the below code:

<Button Name="btnRefreshPortList"
     Grid.Column="1"
     Margin="10 0 0 0"
     Command="{Binding RefreshPortList}" Width="81" Height="53" 
     Click="btnRefreshPortList_Click">

<Image Source="Images\Debug-Outline-icon.png" 
    RenderTransformOrigin=".5,.5"
    Height="40" Width="44">
    <Image.RenderTransform>
        <RotateTransform x:Name="AnimatedRotateTransform" Angle="0" />
    </Image.RenderTransform>
    <Image.Triggers>
        <EventTrigger RoutedEvent="MouseDown">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="AnimatedRotateTransform"                 
                        Storyboard.TargetProperty="Angle" 
                        By="10"        
                        To="{Binding ElementName=slider1, Path=Value+30, UpdateSourceTrigger=PropertyChanged}" 
                        Duration="0:0:0.2" 
                        FillBehavior="HoldEnd" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Image.Triggers>
    </Image>
</Button>

Bind the Angle property of the RotateTransform to the Slider Value :

<Image Source="c:\users\public\pictures\sample pictures\koala.jpg"
       RenderTransformOrigin="0.5,0.5">
    <Image.RenderTransform>
        <RotateTransform Angle="{Binding Value, ElementName=rotationSlider}"/>
    </Image.RenderTransform>
</Image>
<Slider x:Name="rotationSlider" Maximum="90"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM