locked
Silverlight Slider control MouseLeftButtonDown event is not triggered. RRS feed

  • Question

  • Hi.

    I need to use Slider control with MouseLeftButtonDown and MouseLeftButtonUp events in my view model.

    Here is my xaml code:

    <Slider Minimum="0" Maximum="{Binding TotalNumberOfPositions,Mode=TwoWay}" Value="{Binding CurrentPosition, Mode=TwoWay}" Margin="5" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonUp" >
                <i:InvokeCommandAction Command="{Binding StopSeekPosition, Source={StaticResource ViewModel}}" />
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseLeftButtonDown">
                <i:InvokeCommandAction Command="{Binding StartSeekPosition, Source={StaticResource ViewModel}}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Slider>

    When I click the left mouse button StartSeekPosition is not triggered but after I release left button StopSeekPosition is triggered. I have checked the names of commands in view model and there is nothing wrong with naming. Is there a common bug about slider control or my code is wrong? Please help me to solve this issue.


    • Edited by Blast4620 Monday, October 5, 2015 2:06 PM Spelling
    Monday, October 5, 2015 2:03 PM

Answers

  • Hi Blast4620,

    I create a sample Silverlight project with your code, it tell me "The member 'Command' is not recognized or is not accessible". Could your please provide a complete sample code to help us reproduce your problem?
    Please try with attached commands to achieve what you want as below sample code. More information about AttachedBehavior, please refer to following article.
    http://blogs.msdn.com/b/johngossman/archive/2008/05/16/attachedbehavior-pattern-sample.aspx

    public static class ClickBehavior
    {
         ....
         static void element_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
            {
                UIElement element = (UIElement)sender;
                Slider slider = element as Slider;
                   if (slider != null)
                  {
                      double value = slider.Value;
                  }
                
                ICommand command = (ICommand)element.GetValue(ClickBehavior.RightClickCommandProperty);
                command.Execute(value); 
            }
            ...
    }
    public class DelegateCommand : ICommand
    {
        ...
        void ICommand.Execute(object arg)
        {           
                this.handler(arg);
        }
        ...
    }
    public class ViewModel
    {
       ...
       private void DoSomeAction(object arg)
        {
                MessageBox.Show("Command Triggered:" + arg.ToString());
        }  
    }


    Best Regards,
    Weiwei

    • Proposed as answer by Weiwei Cai Monday, October 19, 2015 2:47 PM
    • Marked as answer by Weiwei Cai Tuesday, October 20, 2015 3:30 PM
    Tuesday, October 6, 2015 5:14 AM
  • I user relaycommand with this and I couldn't get either to fire actually.

        <UserControl.DataContext>
            <local:MainPageViewModel/>
        </UserControl.DataContext>
        <Grid x:Name="LayoutRoot" Background="White">
            <Slider Minimum="0" Maximum="100" Value="{Binding CurrentPosition, Mode=TwoWay}" Margin="5" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonUp" >
                        <i:InvokeCommandAction Command="{Binding StopSeekPosition}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseLeftButtonDown">
                        <i:InvokeCommandAction Command="{Binding StartSeekPosition}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Slider>

    and

    using GalaSoft.MvvmLight.Command;
    using System.Diagnostics;
    
    namespace SilverlightApplication1
    {
        public class MainPageViewModel
        {
            public double CurrentPosition { get; set; }
            public RelayCommand StopSeekPosition { get; set; }
            public RelayCommand StartSeekPosition { get; set; }
            public MainPageViewModel()
            {
                StopSeekPosition = new RelayCommand(Stop);
                StartSeekPosition = new RelayCommand(Start);
            }
            private void Stop()
            {
                Debug.WriteLine("Stop handled");
            }
            private void Start()
            {
                Debug.WriteLine("Start handled");
            }
        }
    }

    I don't really follow what you're trying to do here but I suggest you find a different approach.


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    • Proposed as answer by Weiwei Cai Monday, October 19, 2015 2:47 PM
    • Marked as answer by Weiwei Cai Tuesday, October 20, 2015 3:30 PM
    Tuesday, October 6, 2015 9:52 AM

All replies

  • Hi Blast4620,

    I create a sample Silverlight project with your code, it tell me "The member 'Command' is not recognized or is not accessible". Could your please provide a complete sample code to help us reproduce your problem?
    Please try with attached commands to achieve what you want as below sample code. More information about AttachedBehavior, please refer to following article.
    http://blogs.msdn.com/b/johngossman/archive/2008/05/16/attachedbehavior-pattern-sample.aspx

    public static class ClickBehavior
    {
         ....
         static void element_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
            {
                UIElement element = (UIElement)sender;
                Slider slider = element as Slider;
                   if (slider != null)
                  {
                      double value = slider.Value;
                  }
                
                ICommand command = (ICommand)element.GetValue(ClickBehavior.RightClickCommandProperty);
                command.Execute(value); 
            }
            ...
    }
    public class DelegateCommand : ICommand
    {
        ...
        void ICommand.Execute(object arg)
        {           
                this.handler(arg);
        }
        ...
    }
    public class ViewModel
    {
       ...
       private void DoSomeAction(object arg)
        {
                MessageBox.Show("Command Triggered:" + arg.ToString());
        }  
    }


    Best Regards,
    Weiwei

    • Proposed as answer by Weiwei Cai Monday, October 19, 2015 2:47 PM
    • Marked as answer by Weiwei Cai Tuesday, October 20, 2015 3:30 PM
    Tuesday, October 6, 2015 5:14 AM
  • I user relaycommand with this and I couldn't get either to fire actually.

        <UserControl.DataContext>
            <local:MainPageViewModel/>
        </UserControl.DataContext>
        <Grid x:Name="LayoutRoot" Background="White">
            <Slider Minimum="0" Maximum="100" Value="{Binding CurrentPosition, Mode=TwoWay}" Margin="5" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonUp" >
                        <i:InvokeCommandAction Command="{Binding StopSeekPosition}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseLeftButtonDown">
                        <i:InvokeCommandAction Command="{Binding StartSeekPosition}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Slider>

    and

    using GalaSoft.MvvmLight.Command;
    using System.Diagnostics;
    
    namespace SilverlightApplication1
    {
        public class MainPageViewModel
        {
            public double CurrentPosition { get; set; }
            public RelayCommand StopSeekPosition { get; set; }
            public RelayCommand StartSeekPosition { get; set; }
            public MainPageViewModel()
            {
                StopSeekPosition = new RelayCommand(Stop);
                StartSeekPosition = new RelayCommand(Start);
            }
            private void Stop()
            {
                Debug.WriteLine("Stop handled");
            }
            private void Start()
            {
                Debug.WriteLine("Start handled");
            }
        }
    }

    I don't really follow what you're trying to do here but I suggest you find a different approach.


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    • Proposed as answer by Weiwei Cai Monday, October 19, 2015 2:47 PM
    • Marked as answer by Weiwei Cai Tuesday, October 20, 2015 3:30 PM
    Tuesday, October 6, 2015 9:52 AM