I would like to implement textblock with actual value of slider on the top of elipse slider thumb. When textblock have the same widht as elipse everything is working properly but I need to increase the width of textblock.
After width of textloblock changes also width of grid for elipse changed and slider is not looking proper anymore.
Picture of slider with textblock width = elipse width textblock width = elipse width
Textblock width higher than elipse width texblock width higher
Do you have any idea how to avoid situation from picture 2?
Slider thumb style:
<Thumb x:Name="SliderThumb">
<Thumb.Style>
<Style TargetType="Thumb">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb">
<Grid Background="Transparent">
<Ellipse VerticalAlignment="Center" HorizontalAlignment="Center" Fill="Blue" Height="30" Width="30"/>
<TextBlock Width="60" TextAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,80"
HorizontalAlignment="Center" Background="Transparent" Foreground="Black" FontSize="24"
Text="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type Slider}}}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Thumb.Style>
</Thumb>
CodePudding user response:
You have to wrap your TextBlock
in a Canvas
to avoid clipping, and also you need to not set the width for TextBLock
to let it get the size it need.
Here is a working code for your case:
<Thumb x:Name="SliderThumb">
<Thumb.Style>
<Style TargetType="Thumb">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb">
<Grid Background="Transparent" MaxWidth="{Binding ElementName=ellipse, Path=ActualWidth}">
<Ellipse x:Name="ellipse" VerticalAlignment="Center" HorizontalAlignment="Center" Fill="Blue" Height="30" Width="30"/>
<Canvas Margin="0,0,0,80"
Height="{Binding ElementName=valueTextBlock, Path=ActualHeight}"
Width="{Binding ElementName=valueTextBlock, Path=ActualWidth}"
HorizontalAlignment="Center">
<TextBlock x:Name="valueTextBlock"
Background="Transparent"
Foreground="Black"
FontSize="24"
Text="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type Slider}}}"/>
</Canvas>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Thumb.Style>
</Thumb>