如何设置wpf滑块控件拇指的高度

时间:2021-02-16 16:02:14

I want to put a slider in a datagrid cell and the row has a height of 20, so i'd like to make the height of the thumb of the slider smaller than that. I set the height of the slider itself, but the thumb appears to be cut off (i.e. it doesn't scale down to the height that I specify in the slider.height property). I don't want to have to override the entire control template of the slider control to do this. There's got to be some way of setting a property or something like that.

我想在数据网格单元格中放置一个滑块,并且该行的高度为20,所以我想让滑块的拇指高度小于该高度。我设置了滑块本身的高度,但拇指似乎被切掉了(即它没有缩小到我在slider.height属性中指定的高度)。我不想重写滑块控件的整个控件模板来执行此操作。必须有某种方式来设置一个属性或类似的东西。

Edit: Even when I create a custom slider style which includes the custom thumb style with the sizes I want, it still doesn't size right.

编辑:即使我创建了一个自定义滑块样式,其中包含我想要的尺寸的自定义拇指样式,它仍然没有正确的大小。

Any ideas?

3 个解决方案

#1


8  

<Slider.LayoutTransform>
    <ScaleTransform ScaleY="0.9" CenterX="15" CenterY="15"/>
</Slider.LayoutTransform>

Not very sexy, but it works like a charm when combined whith the Slider.Height/Slider.Width properties !

不是很性感,但它与Slider.Height / Slider.Width属性结合时的魅力就像一个魅力!

#2


1  

Set thumb style:

设置拇指样式:

<Style x:Key="SliderThumbStyle" TargetType="{x:Type Thumb}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Thumb}">
                <!--<Ellipse 
                      Name="Ellipse" 
                      Fill="Yellow"
                      Stroke="Yellow" 
                      Height="10"
                      Width="{Binding Path=ThumbWidth, RelativeSource={RelativeSource TemplatedParent}}"
                      StrokeThickness="1" />-->
                <Rectangle 
                    Fill="Azure"
                    Stroke="Azure"
                    Height="7"
                    Width="{Binding Path=ThumbWidth, RelativeSource={RelativeSource TemplatedParent}}"
                    StrokeThickness="1"
                    Margin="0.1,.1,.1,.1"/>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then use this style slider custom control

然后使用此样式滑块自定义控件

<Style TargetType="{x:Type local:NvSliderControl}">
    <Setter Property="Orientation" Value="Vertical" />
    <Setter Property="Height" Value="50"/>        
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:NvSliderControl}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Track x:Name="PART_Track" >
                            <Track.Thumb>
                                <Thumb Style="{StaticResource SliderThumbStyle}">
                                </Thumb>
                            </Track.Thumb>
                        </Track>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

#3


0  

just see this link Binding a slider value on the height of its thumb in WPF

只需看到此链接在WPF中绑定其拇指高度的滑块值

may be this can help you

可能这可以帮到你

#1


8  

<Slider.LayoutTransform>
    <ScaleTransform ScaleY="0.9" CenterX="15" CenterY="15"/>
</Slider.LayoutTransform>

Not very sexy, but it works like a charm when combined whith the Slider.Height/Slider.Width properties !

不是很性感,但它与Slider.Height / Slider.Width属性结合时的魅力就像一个魅力!

#2


1  

Set thumb style:

设置拇指样式:

<Style x:Key="SliderThumbStyle" TargetType="{x:Type Thumb}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Thumb}">
                <!--<Ellipse 
                      Name="Ellipse" 
                      Fill="Yellow"
                      Stroke="Yellow" 
                      Height="10"
                      Width="{Binding Path=ThumbWidth, RelativeSource={RelativeSource TemplatedParent}}"
                      StrokeThickness="1" />-->
                <Rectangle 
                    Fill="Azure"
                    Stroke="Azure"
                    Height="7"
                    Width="{Binding Path=ThumbWidth, RelativeSource={RelativeSource TemplatedParent}}"
                    StrokeThickness="1"
                    Margin="0.1,.1,.1,.1"/>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then use this style slider custom control

然后使用此样式滑块自定义控件

<Style TargetType="{x:Type local:NvSliderControl}">
    <Setter Property="Orientation" Value="Vertical" />
    <Setter Property="Height" Value="50"/>        
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:NvSliderControl}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Track x:Name="PART_Track" >
                            <Track.Thumb>
                                <Thumb Style="{StaticResource SliderThumbStyle}">
                                </Thumb>
                            </Track.Thumb>
                        </Track>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

#3


0  

just see this link Binding a slider value on the height of its thumb in WPF

只需看到此链接在WPF中绑定其拇指高度的滑块值

may be this can help you

可能这可以帮到你