views:

502

answers:

2

Hi, I have a databinding problem in WPF.

I would like to "customise" a slider in a way that the thumb grows when you move the slider to the right and the thumb shrinks when you move the slider to the left.

So I edited the template for the slider and changed the look of the slider so the slider looks like I want it to.

But now I have to bind the height of the thumb to the value of the slider but I do not know how that works.

I did some simple data binding things but I cannot figure out how I can bind this "thumb height" that's inside of my slider's template to the slider's value that's inside the User Control where my slider is in.

So how can I do it?

A: 

Check my question and answer here -

http://stackoverflow.com/questions/664476/in-wpf-xaml-how-do-i-change-the-size-of-a-paragraph-of-text-using-a-scroll-bar

If you have something like this:

<ScrollBar x:Name="scroll1"></ScrollBar>

<Image Height="{Binding ElementName=scroll1, Path=Value}" />

NB I'm not 100% sure of the syntax for the image, so you'll need to take this as pseudo code.

ChrisF
+1  A: 

You can use RelativeSource binding (Height="{Binding Value, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Slider}}}").

Here is an example of the thumb style, that depends on Slider.Value:

<Style x:Key="SliderThumbStyle" TargetType="{x:Type Thumb}">
  <Setter Property="SnapsToDevicePixels" Value="true"/>
  <Setter Property="OverridesDefaultStyle" Value="true"/>
  <Setter Property="Width" Value="14"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Thumb}">
        <Ellipse 
          Name="Ellipse" 
          Fill="#C0C0C0"
          Stroke="#404040" 
          Height="{Binding Value, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Slider}}}"
          StrokeThickness="1" />
        <ControlTemplate.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="Ellipse" Property="Fill" Value="#808080"/>
          </Trigger>
          <Trigger Property="IsEnabled" Value="false">
            <Setter TargetName="Ellipse" Property="Fill" Value="#EEEEEE"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Hope this helps.

Cheers Anvaka

Anvaka
Thanks for you Answer. It works!The only thing I didn't know was, that the slider-track drops down when the thumb grows because the track has to stay vertically in the center of the thumb and when the thumb grow the track has to move down. So I cannot you it because it doesn't look nice...
sofri