Search code examples
c#wpfmvvmdevexpressdevexpress-wpf

Slider ValueChanged method command ( MVVM WPF Devexpress )


I'm trying to bind a method to the value changed of a slider. I'm using devexpress poco,

XAML:

<ListView.ItemTemplate>
     <DataTemplate>
          <StackPanel Height="140" Margin="20">
               <Slider Height="100" Width="40" Margin="5" HorizontalAlignment="Left" TickFrequency="10" TickPlacement="BottomRight" Orientation="Vertical" Minimum="0" Maximum="100" Value="{Binding VolumeLevel}">
                    <dxmvvm:Interaction.Behaviors>
                         <dxmvvm:EventToCommand EventName="ValueChanged" Command="{Binding Path=VolumeChangedCommand}" />
                    </dxmvvm:Interaction.Behaviors>
               </Slider>
               <TextBlock Text="{Binding Name}" />
          </StackPanel>
     </DataTemplate>
</ListView.ItemTemplate>

My C# Code:

public void VolumeChanged()
{
     ...
}

The method never gets called.
Any suggestions?


Solution

  • Because you are binding the slider to a Command (this line: <dxmvvm:EventToCommand EventName="ValueChanged" Command="{Binding Path=VolumeChangedCommand}" />), and you don't have a Command so it won't fire. All you need to do is add a public Command VolumeChangedCommand

    private ICommand _VolumeChangedCommand;
    public ICommand VolumeChangedCommand
    {
        get
        {
            if (_VolumeChangedCommand == null)
                _VolumeChangedCommand = new CommandImplement();
            return _VolumeChangedCommand ;
        }
        set
        {
            _VolumeChangedCommand = value;
        }
    }
    
    class CommandImplement: ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        } 
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        } 
    
        public void Execute(object parameter)
        {
             VolumeChanged(); //Call your method or put your code here.
        }
    }