3

I have a WPF application with Caliburn.Micro.

I want to handle slider's move, i.e. MouseUp event.

<Slider cal:Message.Attach="[Event MouseUp] = [Action OnSliderMouseUp($this)]"  
        Value="{Binding  PlayerPosition, Mode=OneWay}" MinWidth="200" 
        VerticalAlignment="Center" Minimum="0" 
        Maximum="{Binding Player.NaturalDuration.TimeSpan.TotalMilliseconds}"/>

In ViewModel:

public void OnSliderMouseUp(Slider slider)
{
    int blah = 9;
}

OnSliderMouseUp() is never called. Could you please tell what I am missing?

nemesv
  • 138,284
  • 16
  • 416
  • 359
David Shochet
  • 5,035
  • 11
  • 57
  • 105
  • 1
    Have you wired your view and viewmodel with Caliburn? How do you set the DataContext to your viewmodel? Try adding `cal:Action.TargetWithoutContext="{Binding}"` to your `Slider` if it solves your problem it means your DataContext is not correctly wired with Caliburn – nemesv Sep 07 '12 at 20:20
  • I follow the naming convention, so I assume the DataContext is the ViewModel. I added the code you suggested, but it still doesn't work. – David Shochet Sep 10 '12 at 11:30

1 Answers1

9

Actually you have two problems:

  1. The Slider control does not fires the MouseUp event. If you interested in an event which fires when the user stops dragging the slider you are looking for the Thumb.DragCompleted. You can find more info here: WPF: Slider with an event that triggers after a user drags

  2. But if you whould write

    <Slider cal:Message.Attach="[Event Thumb.DragCompleted] = [Action OnSliderMouseUp($this)]"  />
    

    it still won't work. Because Caliburn.Micro (to be preciese System.Windows.Interactiviy.EventTrigger which is used by Calibrun.Micro) does not support attached events. For more info and a workaround see: using attached events with caliburn micro Message.Attach

So a working soultion (with the RoutedEventTrigger implementation from the above mentioned question):

<Slider Value="{Binding  PlayerPosition, Mode=OneWay}" MinWidth="200" ...>
    <i:Interaction.Triggers>                
        <local:RoutedEventTrigger RoutedEvent="Thumb.DragCompleted">
            <cal:ActionMessage MethodName="OnSliderMouseUp">
                <cal:Parameter Value="$source" />
            </cal:ActionMessage>
        </local:RoutedEventTrigger>
    </i:Interaction.Triggers>
</Slider>

Note that because Thumb.DragCompleted an attached event $this won't work and you need to use $source instead.

Community
  • 1
  • 1
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Thank you very much for your answer. The execution now comes to the handler, but the parameter is null. Could you tell why it is so? Thanks. – David Shochet Sep 10 '12 at 13:24
  • I've also realized that and updated my answer see the note at the end. You need use `$source` instead of `$this` (or $eventArgs and get the slider form the `DragComplatedEventArgs`'s `Source` property) – nemesv Sep 10 '12 at 13:27
  • Strange it works for me with `$source`. Please try it with `$eventArgs` but in this case your method should look like: `OnSliderMouseUp(DragCompletedEventArgs args)` then the `args.Source` should contain your slider. If not then please post it what is inside the `args.Source` and `args.OriginalSource` – nemesv Sep 10 '12 at 13:31
  • what is `i:` namespace and `local:` namespace? – M.kazem Akhgary Dec 26 '18 at 10:16
  • 1
    @M.kazemAkhgary i is `xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ` local is your own namespace where you have your `RoutedEventTrigger` implementation described here https://stackoverflow.com/questions/8402339/using-attached-events-with-caliburn-micro-message-attach – nemesv Dec 26 '18 at 10:20