自定义 WPF 滑块,图像为拇指

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25136114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:19:55  来源:igfitidea点击:

Custom WPF Slider with image as Thumb

wpfxamlslider

提问by Jibin Mathew

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

如何以如下图所示的方式为 WPF 设置 Slider 控件的样式:

enter image description here

在此处输入图片说明

Any similar samples would be appreciated.

任何类似的样品将不胜感激。

I tried the below code

我试过下面的代码

  <Style x:Key="SliderThumbStyle" TargetType="Thumb">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="false"/>
        <Setter Property="Height" Value="18"/>
        <Setter Property="Width" Value="18"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Thumb">
                    <Ellipse  Stroke="Black"
                          StrokeThickness="1"
                          Name="Ellipse" 
                          Fill="OrangeRed" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="Ellipse" Property="Fill" Value="Orange"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Ellipse" Property="Fill" Value="Gray"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="Slider" x:Key="AppSliderStyle">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Slider">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" Name="row" />
                            <RowDefinition Height="auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto" />
                            <ColumnDefinition Width="*" Name="column" />
                            <ColumnDefinition Width="auto" />
                        </Grid.ColumnDefinitions>
                        <Border Name="PART_Border"
                           BorderBrush="Black" BorderThickness="1"
                           Padding="2"
                          Grid.Row="1" Grid.Column="1"
                           Width="{TemplateBinding Width}"
                           Height="{TemplateBinding Height}"
                           Background="GreenYellow"
                           HorizontalAlignment="Stretch"
                           VerticalAlignment="Center" />
                        <Track Name="PART_Track"
                              HorizontalAlignment="Stretch"
                              VerticalAlignment="Center"
                              Grid.Row="1" Grid.Column="1"
                              Width="{TemplateBinding Width}"
                              Height="{TemplateBinding Height}">
                            <Track.Thumb>
                                <Thumb Style="{StaticResource SliderThumbStyle}" />
                            </Track.Thumb>
                        </Track>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And it produced this :

它产生了这个:

enter image description here

在此处输入图片说明

I am struggling to make the color different on the left and right sides. What changes do I have to make to do that?

我正在努力使左侧和右侧的颜色不同。我必须做出哪些改变才能做到这一点?

回答by Heena Patil

Similar example :

类似的例子:

Track bar /slider template for WPF

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>

Updateslider 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

在此处输入图片说明

回答by Code Scratcher

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

在此处输入图片说明在 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

单击此处获取带有源代码的完整解决方案