mimic
mimic

Reputation: 5224

Silverlight event handling

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" 
        MouseLeftButtonDown="sliderTime_MouseLeftButtonDown" 
        MouseLeftButtonUp="sliderTime_MouseLeftButtonUp" />

And in code behind:

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

Is it bug of the slider or something else? Thanks

Upvotes: 1

Views: 1182

Answers (3)

KeithMahoney
KeithMahoney

Reputation: 3343

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

Upvotes: 0

user179700
user179700

Reputation: 522

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.";
    }

Upvotes: 1

AASoft
AASoft

Reputation: 1344

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

Upvotes: 0

Related Questions