1

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?

2
  • Where is your method defined ? In which DataContext ? Assuming your current XAML code, it has to be defined in the model/item class. (Which would be in ListView.ItemsSource[i].VolumeChangedCommand) - Also your Command has to implement the ICommand interface. Commented Aug 28, 2018 at 4:30
  • ValueChanged is the name of the event. Your VolumeChangedCommand should get invoked whenever this event is raised. Commented Aug 28, 2018 at 15:27

1 Answer 1

1

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.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.