Click here to Skip to main content
15,063,085 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi All!

My goal is to create custom slider where sliders current value is shown inside its dragable thumb (inside textblock). Im quite new in wpf, but I think it is doable with styles?

Here is my current thumbstyle. The problem is that I cannot bind my textbox Text property to sliders Value property (slider style x:key is MySlider). Or is it event right way to do it...

<!--Simple SliderThumb - The Thumb is the draggable part of a Slider--> 
<Style x:Key="MySliderThumb" d:IsControlPart="True" TargetType="{x:Type Thumb}">
<setter property="SnapsToDevicePixels" value="true"></setter>
<setter property="Height" value="50"></setter>
<setter property="Width" value="50"></setter>
<setter property="Template">
  <setter.value>
    <controltemplate targettype="{x:Type Thumb}">
      <grid>                        
         <border borderbrush="{DynamicResource normalBorderBrush}" borderthickness="2">

         <textblock text="{Binding ElementName=MySlider, Path=Value}"></textblock> <!--THIS WONT WORK. HOW TO BIND MY SLIDER STYLE x:key="MySlider" AND ITS CURRENT VALUE?-->          
         </border>
     </grid> 
                </controltemplate>
            </setter.value>
        </setter>
    </Style>


Hope you got idea :)

Cheers!
Posted

1 solution

Try
<textblock text="{TemplateBinding Value}" />


Note that I'm not sure if this is actually possible with this control (I've not tried and I don't use WPF/Silverlight much). That seems like the appropriate binding path though.
   
Comments
paleGegg0 16-Aug-11 7:26am
   
Hi! Thanks for your time! Im afraid it did not do the trick because thumb template has not property called Value. Instead Slider which will use the thumb has property called Value and that is the propery what i like to bind to thumbs textblock.
BobJanova 16-Aug-11 8:33am
   
Hm, I see. And the slider is the parent of the thumb? Can you bind to {TemplateBinding Parent.Value}, or something similar?
paleGegg0 17-Aug-11 0:19am
   
Im afraid that does not work neither.. Anyway good thougths overall, I have tried many variations without success. Maybe it is the case that normal bindings wont work with styles..
BobJanova 17-Aug-11 14:57pm
   
You might need to redefine the template for the whole slider (which includes the thumbs, I assume?), at which point 'TemplateBinding' should apply to slider properties and my first attempt should work. That might be a lot of work though if it's a complex template. It seems that there should be a way to do what you want, but I don't know what it is, sorry.
paleGegg0 18-Aug-11 3:03am
   
Refining whole slider did the trick! Here is what did in nutshell
mySliderStyle

<Thumb Tag="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}" Style="{DynamicResource myThumbStyle}"/>

paleGegg0 18-Aug-11 4:10am
   
and in myThumbStyle
textblock

text="{Binding RelativeSource={RelativeSource Self}, Path=DataContext}" datacontext="{TemplateBinding Tag}">
Amazing! Thanks :)
BobJanova 18-Aug-11 12:47pm
   
Glad you got something working!

TemplatedParent is not available if you only override the thumb?
paleGegg0 18-Aug-11 13:49pm
   
Im quite happy with current solution (workaround). I tried many variations without success but maybe i missed something.. anyway refining slider with its custom thumb did the trick as you suggested. Xaml is truly powerful but difficult master..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900