开发者

"Spring" wpf slider control

开发者 https://www.devze.com 2023-04-05 00:06 出处:网络
how can开发者_开发问答 i make in Wpf 4 -\"Spring\"-slider control? the Thumb alwaysback to set point 0,

how can开发者_开发问答 i make in Wpf 4 -"Spring"-slider control? the Thumb always back to set point 0, after Release the Thumb , it jump like "spring" to the beginning. Cheers Diu


You can use triggers to do this in XAML (which is just springing back on the release of the thumb with a duration of a second)

        <Slider x:Name="slider">
        <Slider.Triggers>
            <EventTrigger SourceName="slider" RoutedEvent="Thumb.DragCompleted">    
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="slider" Storyboard.TargetProperty="Value" From="{Binding Value, ElementName=slider}" To="0" Duration="0:0:1"/>
                    </Storyboard>
                </BeginStoryboard>          
            </EventTrigger>
        </Slider.Triggers>              
    </Slider>

However, this won't capture clicks on the slider for steps or direct setting of the Value property. To fully capture this you would need to listen to all three events (Thumb.DragCompleted, Thumb.DragStarted and Slider.ValueChanged) and make sure that you don't apply any animations while the thumb is being dragged. You would also want to customise the duration of the animation as currently it has a constant time, rather than constant speed.

Although it would be possible to do fully within XAML, you may find it easier to use code-behind to achieve a fully functional implementation of a 'springy' slider.

0

精彩评论

暂无评论...
验证码 换一张
取 消