Custom WPF Slider with image as Thumb

后端 未结 2 718
夕颜
夕颜 2020-12-01 07:08

How can I style a Slider control for WPF in a way like this figure:

\"enter

An

相关标签:
2条回答
  • 2020-12-01 07:19

    enter image description herecreate custom thumb / track bar / slider control template in WPF

    <Ellipse Name="PART_Ellipse" 
                             Width="30"
                             Height="30" 
                             Stroke="Black"
                             Fill="{StaticResource RoundButtonDefaultBrush}"/>
                <ContentPresenter Name="PART_ContentPresenter"  ContentSource="Content" Margin="0,0,0,6" 
                                      HorizontalAlignment="Center" 
                                      VerticalAlignment="Center"
                                      TextBlock.Foreground="White"
                                      TextBlock.FontSize="18"
                                      TextBlock.FontWeight="Bold"/>
    

    Check the complete code here:

    Click here for complete solution with source code

    0 讨论(0)
  • 2020-12-01 07:39

    Similar example :

    Track bar /slider template for WPF

    You need to edit style of Both RepeatButton

    <Window.Resources>
    
        <Style x:Key="SliderRepeatButton" TargetType="RepeatButton">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="IsTabStop" Value="false" />
            <Setter Property="Focusable" Value="false" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RepeatButton">
                        <Border  BorderThickness="1" BorderBrush="Black" Background="Black" Height="3"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <Style x:Key="SliderRepeatButton1" TargetType="RepeatButton">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RepeatButton">
                        <Border SnapsToDevicePixels="True" Background="Green"  BorderThickness="1" BorderBrush="YellowGreen" Height="3"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <Style x:Key="SliderThumb" TargetType="Thumb">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Thumb">
                        <Ellipse Height="10" Width="10" Fill="Green"></Ellipse>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <ControlTemplate x:Key="Slider"  TargetType="Slider">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Track Grid.Row="1" x:Name="PART_Track"   >
                    <Track.DecreaseRepeatButton>
                        <RepeatButton Style="{StaticResource SliderRepeatButton1}"  Command="Slider.DecreaseLarge" />
                    </Track.DecreaseRepeatButton>
                    <Track.Thumb>
                        <Thumb Style="{StaticResource SliderThumb}"  />
                    </Track.Thumb>
                    <Track.IncreaseRepeatButton>
                        <RepeatButton Style="{StaticResource SliderRepeatButton}" Command="Slider.IncreaseLarge" />
                    </Track.IncreaseRepeatButton>
                </Track>
            </Grid>            
        </ControlTemplate>
    
        <Style x:Key="Horizontal_Slider" TargetType="Slider">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Style.Triggers>
                <Trigger Property="Orientation" Value="Horizontal">
                    <Setter Property="MinHeight" Value="21" />
                    <Setter Property="MinWidth" Value="104" />
                    <Setter Property="Template" Value="{StaticResource Slider}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <Slider Style="{StaticResource Horizontal_Slider}" VerticalAlignment="Center"  Value="500" Width="300" Margin="50,0,50,0"></Slider>
    

    Update slider thumb with image

       <Style x:Key="SliderThumb" TargetType="Thumb">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Thumb">
                        <Ellipse Height="10" Width="10">
                            <Ellipse.Fill>
                                <ImageBrush ImageSource="Screenshot_5.png"></ImageBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    Update: slider thumb with Mouseover animation

    You can add mouserover effect using triggers.

    <Style x:Key="SliderThumb" TargetType="Thumb">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Thumb">
                        <!--Add name to ellipse to use in  controltemplate triggers-->
                        <Ellipse x:Name="Ellipse" Height="10" Width="10" Fill="Green"></Ellipse>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="Ellipse" Property="Fill" Value="Yellow"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    Result

    enter image description here

    0 讨论(0)
提交回复
热议问题