Ivan Ivan - 2 months ago 14
C# Question

Slider's value lags its thumb's current position

I have a slider like this:

<Slider x:Name ="slider_logo_scale" IsMoveToPointEnabled="True" Style="{StaticResource MyCustomStyleForSlider}" Grid.Row="76" Grid.Column="22" Grid.ColumnSpan="31" Grid.RowSpan="3" SmallChange="1" Value="{Binding LogoScaleValue}" Maximum="5" IsEnabled="{Binding ControlsEnabled}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction Command="{Binding SetLogoSizeCommand}" CommandParameter="{Binding Value, ElementName=slider_logo_scale}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Slider>


and the viewmodel:

public class ViewModel
{
public double LogoScaleValue { get; set; }


public CompositeCommand SetLogoSizeCommand { get; set; }

public ViewModel()
{
SetLogoSizeCommand = new CompositeCommand();
SetLogoSizeCommand.RegisterCommand(new DelegateCommand<Double?>(SetLogoSize));
}


private void SetLogoSize(Double? argument)
{

}

}


When I drag the slider, everything works perfect. However, when I click on the slider, the thumb snaps to the correct position, and 'SetLogoSize(Double? argument)' gets called but 'argument' has the previous value of the slider's thumb, before it jumped to this new position. This causes the value returned by the slider to lag the thumb's current position.

Any idea how to solve this?

Answer

Use the singleton pattern and only call the SetLogoSize(Double? argument) method when the LogoScaleValue is set.

public class ViewModel
{       
    private double _logoScaleValue;

    public double LogoScaleValue 
    { 
        get{return _logoScaleValue; }
        set
           {
             _logoScaleValue = value;
             SetLogoSize(value);
            }
    }


    public CompositeCommand SetLogoSizeCommand { get; set; }

    public ViewModel()
    {
        SetLogoSizeCommand = new CompositeCommand();
        SetLogoSizeCommand.RegisterCommand(new DelegateCommand<Double?>(SetLogoSize)); 
    }


    private void SetLogoSize(Double? argument)
    {

    }

}
Comments