How do I set the height of a wpf slider control's thumb

喜欢而已 提交于 2019-12-07 05:13:27

问题


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.

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?


回答1:


<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 !




回答2:


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>


来源:https://stackoverflow.com/questions/3619799/how-do-i-set-the-height-of-a-wpf-slider-controls-thumb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!