vjsakec99

Hi,

I have created a slider with TickBar and now I wanted to display numbers besides those ticks.If anybody can help me out then it would be great.

Thanks




Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

Josh Smith

You can use a TextBlock to display the number, and then bind it's Text property to the Value property of the slider. This article shows how to do that, in the "Displaying the angle of rotation" section: http://www.codeproject.com/WPF/GuidedTourWPF_3.asp

HTH






Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML


Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

vjsakec99

Hi,

see what i have is a calibrated vertical slider and like i have 7 ticks and I want to display ticks like bottom tick will have value 0%, the one above that will have 25 % and so on.SO is there anyway I can do that using XAML.

Thanks






Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

Josh Smith

I think that the link lee supplied is more in line with what you want to do. You won't be able to easily do that in XAML.




Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

vjsakec99

How can I override the OnRender method to dispaly the numbers Any examples would be a great help




Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

Tamir Khason

I would advice you to create custom control template, rather overriding OnRender, however, overriding default control methods is not much different between WPF and WinForms




Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

lee d

something along those lines

public class MyTickBar : TickBar
{
protected override void OnRender(DrawingContext dc)
{
Size size = new Size(base.ActualWidth, base.ActualHeight);
double num = this.Maximum - this.Minimum;
Point point = new Point(0, 0);
Point point2 = new Point(0, 0);
double y = this.ReservedSpace * 0.5;
FormattedText formattedText = null;
for (int i = 0; i < 10; i++)
{
formattedText = new FormattedText(
i.ToString(),
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
8,
Brushes.Black);

dc.DrawText(formattedText, new Point((i * (size.Width/10)),0));
}
}
}






Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

vjsakec99

I am confused to override the function or write custom slider help me out.

But This is what I am looking for

I have a sliders(group of 6) and each slider has min value , max values, different values and different number ticks

like the one below I have 5 more and I want to display the ticks value on each slider

<Slider Height="150" Width="50" Background="LightGray" Name="sldconversion" Orientation="Vertical" TickPlacement="BottomRight" IsDirectionReversed="False"
HorizontalAlignment="Center" IsEnabled="{Binding ElementName=rbconversion,Path=IsChecked,Mode=TwoWay}" Minimum="0" Maximum="8" Ticks="0,1,2,3,4,5,6,7,8"/>

Thanks






Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

lee d

You dont need to write custom slider.

get the slider template from the SDK and in that it uses tickbar. replace that tickbar with your derived tickbar(sample above)






Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

vjsakec99

I have a doubt should I override the Tickbar in the XAML file or the class file.And if you could let me know how it can be done

Thanks






Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

lee d

this is a very rough implementation(gives an idea)

Code Snippet

<ControlTemplate x:Key="HorizontalSlider" TargetType="{x:Type Slider}">

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto" MinHeight="{TemplateBinding Slider.MinHeight}"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<local:MyTickBar

Name="TopTick"

SnapsToDevicePixels="True"

Placement="Top"

Fill="{StaticResource GlyphBrush}"

Height="5"

/>

<Border

Name="TrackBackground"

Margin="0"

CornerRadius="2"

Height="4"

Grid.Row="1"

Background="{StaticResource LightBrush}"

BorderBrush="{StaticResource NormalBorderBrush}"

BorderThickness="1" />

<Track Grid.Row="1" Name="PART_Track">

<Track.DecreaseRepeatButton>

<RepeatButton

Style="{StaticResource SliderButtonStyle}"

Command="Slider.DecreaseLarge" />

</Track.DecreaseRepeatButton>

<Track.Thumb>

<Thumb Style="{StaticResource SliderThumbStyle}" />

</Track.Thumb>

<Track.IncreaseRepeatButton>

<RepeatButton

Style="{StaticResource SliderButtonStyle}"

Command="Slider.IncreaseLarge" />

</Track.IncreaseRepeatButton>

</Track>

<TickBar

Name="BottomTick"

SnapsToDevicePixels="True"

Grid.Row="2"

Fill="{TemplateBinding Foreground}"

Placement="Bottom"

Height="4"

Visibility="Collapsed" />

</Grid>

<ControlTemplate.Triggers>

<Trigger Property="TickPlacement" Value="TopLeft">

<Setter TargetName="TopTick" Property="Visibility" Value="Visible"/>

</Trigger>

<Trigger Property="TickPlacement" Value="BottomRight">

<Setter TargetName="BottomTick" Property="Visibility" Value="Visible"/>

</Trigger>

<Trigger Property="TickPlacement" Value="Both">

<Setter TargetName="TopTick" Property="Visibility" Value="Visible"/>

<Setter TargetName="BottomTick" Property="Visibility" Value="Visible"/>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

<Slider Template="{StaticResource HorizontalSlider}" Width="300" Height="50" Maximum="10" Value="5" TickPlacement="BottomRight" TickFrequency="1" Minimum="0"></Slider>

public class MyTickBar : TickBar

{

protected override void OnRender(DrawingContext dc)

{

Size size = new Size(base.ActualWidth, base.ActualHeight);

double num = this.Maximum - this.Minimum;

Point point = new Point(0, 0);

Point point2 = new Point(0, 0);

double y = this.ReservedSpace * 0.5;

FormattedText formattedText = null;

for (int i = 0; i < 10; i++)

{

formattedText = new FormattedText(

i.ToString(),

CultureInfo.GetCultureInfo("en-us"),

FlowDirection.LeftToRight,

new Typeface("Verdana"),

8,

Brushes.Black);

dc.DrawText(formattedText, new Point((i * (size.Width / 10)), 30));

}

}

}






Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

vjsakec99

When I try to use Local node it is giving me an error that it does not recognize this node.




Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

lee d

it is just a mapping added to the window/page

xmlns:local="clr-namespace:your namespace"






Re: Windows Presentation Foundation (WPF) Display numbers on the slider using XAML

vjsakec99

I added the namespace and it is still giving me error as it is not able to recognize local:MyTickBar