views:

506

answers:

4

Hello everyone, I try to catch the MouseUp event from the slider but it never goes inside of the handler. The code is usual:

<Slider x:Name="sliderTime" Minimum="0" MouseLeftButtonDown="sliderTime_MouseLeftButtonDown" MouseLeftButtonUp="sliderTime_MouseLeftButtonUp" Width="Auto" Height="20" Margin="5" >
                    </Slider>

private void sliderTime_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //some code
        }

Is it bug of the slider or something else? Thanks

A: 

Since Silverlight 2 Beta 2, many controls dont fire the MouseDown/MouseUp events, and I believe that Slider is one of them. You can "work around" that by inheriting from Slider and writing custom code to fire these events. Take a look here: http://forums.silverlight.net/forums/p/18328/61917.aspx

AASoft
Hmmm very interesting but you know slider fires MouseDown but not MouseUp, it's the reason I thought that it can be a bug. What do you think?
Seacat
A: 

I duplicated the code you posted and it worked for me.

Any chance the event isn't actually wired up?

I built it two different ways and the event fired for me. How did you build it?

Also check to see if you somehow have an object covering the slider?

Version 1: Open Blend 3, file>create project, Silverlight 3 Application + Website. Added a slider and named it (simply because you did). Added a label (to check the event firing).

Selected the component, switched over events and double-clicked the events for MouseLeftButtonDown and MouseLeftButtonUp to create the events and code-behind stubs. Updated the label when MouseLeftButtonUp fires.

Version 2: Open Blend 3, file>create project, Silverlight 3 Application + Website. Added a slider and a label. Right-clicked on the silverlight project in Blend and opened it in VS2008. Wired up the events in markup using intellisense.

Both version worked for me. Is this part of other code? If so try to make a version with just the slider and see if that works, if it does then something in your existing code may be off. I'll post my code so you can see it.

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"
    x:Class="SilverlightAppSlider2Test.MainPage"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" Background="White">
      <Slider x:Name="sliderTime" MouseLeftButtonUp="Slider_MouseLeftButtonUp" MouseLeftButtonDown="sliderTime_MouseLeftButtonDown" Width="Auto" Height="20" Margin="5"/>
      <dataInput:Label x:Name="Label1" Width="200"/>
    </Grid>
</UserControl>

AND

 private void Slider_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
 {
  // TODO: Add event handler implementation here.
  Label1.Content = "Mouse button left released.";
 }
A: 

You can use the FrameworkElement.AddHandler method to handle this event. You won't be able to wire it up in xaml however. More info in this blog post:

http://blogs.msdn.com/kmahone/archive/2009/03/19/handling-mouse-input-events-on-controls.aspx

KeithMahoney
A: 

I have exactly the same problem with OnMouseLeftButtonDown on the slider (silverlight 3)

I tried using but that wasnt firing either, OnMouseLeftButtonUp works fine. -im stumped

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);
        e.Handled = false;
    }
lex

related questions