views:

409

answers:

1

When trying to use a Slider control I'd like to listen to the MouseLeftButtonDown and MouseLeftButtonUp. I have handlers set up for both events. The MouseLeftButtonUp works as expected. MouseLeftButtonDown is not raised at all.

Any ideas why?

I've done a bit of googling and it seems that the WPF doesn't fire either. One of the solutions (in this post) was to use the Preview version of the events which is something silverlight doesn't support.

Is there any simple solution to this that I'm not seeing?

Thanks J

+4  A: 

Hi James,

It happens because Slider handles mouse down/up events. Internally its implemented as two RepeatButtons and a thumb in the middle. When you click on left or right side of the slider your mouse events are handled by RepeatButtons, and you don't get them.

If you still want to handle handled event you can use AddHandler() method. Here is Silverlight example:

XAML

<Slider Width="100"
        VerticalAlignment="Top"
        Minimum="0"
        Maximum="100"
        Name="sl" />

C#

public partial class MainPage : UserControl
{
  public MainPage()
  {
    InitializeComponent();

    sl.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(Slider_MouseLeftButtonDown), true);
    sl.AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(Slider_MouseLeftButtonUp), true);
  }

  private void Slider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  {
  }

  private void Slider_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  {
  }
}

In WPF situation is almost same (small differences in names).

Anvaka