自定义WPF滑块与三角拇指

时间:2015-10-22 03:50:55

标签: c# wpf slider customization

我的目标是制作一个这样的滑块:

Desired Slider

问题是,当我将滑块移动到它的最大值时,三角形的点不会达到最大值的顶部,它保持如下:

Max value

我相信它的原因是因为Thumb's位置仅限于它的大小所以它不能超出滑块的限制....但在我的这正是我想要实现的!我尝试更改Thumb高度,但这可能有助于Top,但这会在滑块的Bottom中出现问题。

我的代码中缺少什么?

到目前为止我的代码是:

The Slyder:

<Slider Grid.Row="1"
        Grid.Column="1"
        Maximum="1000" 
        Orientation="Vertical" 
        HorizontalAlignment="Center"
        Margin="20,0,20,20"
        Height="150" 
        Style="{DynamicResource LampadaSliderStyle}" />

重复按钮样式:

<Style x:Key="SliderRepeatButtonStyle" TargetType="{x:Type RepeatButton}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="IsTabStop" Value="false"/>
    <Setter Property="Focusable" Value="false"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RepeatButton}">
                <Rectangle Fill="Transparent"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

拇指样式:

<Style x:Key="LampadaThumbStyle" TargetType="{x:Type Thumb}">
    <Setter Property="Focusable" Value="false"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Height" Value="20"/>
    <Setter Property="Width" Value="20"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Thumb}">
                <Canvas SnapsToDevicePixels="true">                        
                    <Path Data="M8.1965079,-17.994989 L28.196667,-28.213333 28.196333,-8.213772 z" 
                          Fill="Black" 
                          Height="20" 
                          Canvas.Left="23.197" 
                          Stretch="Fill" 
                          Canvas.Top="9.768" 
                          Width="20"/>
                </Canvas>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

滑块样式:

<Style x:Key="LampadaSliderStyle" TargetType="{x:Type Slider}">
    <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Slider}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition MinWidth="{TemplateBinding MinWidth}" Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <UniformGrid x:Name="TrackBackground" 
                                     Rows="6" 
                                     Columns="0">
                            <Rectangle Fill="Red" />
                            <Rectangle Fill="Orange" />
                            <Rectangle Fill="Yellow" />
                            <Rectangle Fill="LightGreen" />
                            <Rectangle Fill="Green" />
                            <Rectangle Fill="DarkGreen" />
                        </UniformGrid>
                        <Canvas Margin="-1,-6">
                            <Rectangle x:Name="PART_SelectionRange" 
                                               Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" 
                                               Stroke="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" 
                                               StrokeThickness="1.0" 
                                               Visibility="Hidden" 
                                               Width="4.0"/>
                        </Canvas>
                        <Track x:Name="PART_Track">
                            <Track.DecreaseRepeatButton>
                                <RepeatButton Command="{x:Static Slider.DecreaseLarge}" Style="{StaticResource SliderRepeatButtonStyle}"/>
                            </Track.DecreaseRepeatButton>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton Command="{x:Static Slider.IncreaseLarge}" Style="{StaticResource SliderRepeatButtonStyle}"/>
                            </Track.IncreaseRepeatButton>
                            <Track.Thumb>
                                <Thumb x:Name="Thumb" Style="{DynamicResource LampadaThumbStyle}"/>
                            </Track.Thumb>
                        </Track>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我通过编辑默认模板来制作它们。

1 个答案:

答案 0 :(得分:3)

您可以做的最简单的调整是设置10的上边距(Path的高度的一半),以便将轨道背景放下足以让三角形笔尖匹配:

<UniformGrid x:Name="TrackBackground" Rows="6" Columns="0" 
             Margin="0,10,0,0">
     <!-- ... -->
</UniformGrid>

UniformGrid的位置超过1个,因此请相应地进行编辑。