How to create slider with "progress bar" or indicator WPF

Code Wanderer 396 Reputation points
2020-09-22T15:15:23.687+00:00

I want change style of slider with indicator (indicator = something which give user visual how distance is slider moved from center of slider - for example)

The edit style is easy but I don't know if it is possible to custom change the behavior of style (for example add some code in codebehind). I want create slider with minimum value -1 and max value 1 and when slider is Value < 0.0 or > 0.0 indicator will have size from center to slider. (look at image)

26552-ormnn.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2020-09-23T06:58:24.45+00:00

    To make the Thumb to a circle , you can edit it's default Grid part of Template[Doucument outline >Edit Templete>Edit a copy... ] named SliderThumbHorizontalDefault to below code:

     <Grid HorizontalAlignment="Center" UseLayoutRounding="True" VerticalAlignment="Bottom" Width="20" Height="20">  
                    <Path x:Name="grip" Width="20" Height="20" Fill="Red" SnapsToDevicePixels="True" Stroke="Red" UseLayoutRounding="True" VerticalAlignment="Center" HorizontalAlignment="Center">  
                        <Path.Data>  
                            <EllipseGeometry Center="10,10" RadiusX="10" RadiusY="10"></EllipseGeometry>  
                        </Path.Data>  
                    </Path>  
                </Grid>  
    

    Then set IsSelectionRangeEnabled="True" ValueChanged="MySilder_ValueChanged" for the Slider.The MySilder_ValueChanged code is:

     private void MySilder_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)  
            {  
                if(MySilder.Value>0)  
                {  
                    MySilder.SelectionEnd = MySilder.Value;  
                    MySilder.SelectionStart = 0;  
                }else  
                {  
                    MySilder.SelectionEnd = 0;  
                    MySilder.SelectionStart = MySilder.Value;  
                }  
            }  
    

    The result picture is like this:
    26801-4.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful