Reputation: 1171
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?
Upvotes: 2
Views: 98
Reputation: 45262
Why have the command at all? It is much simpler if you handle it inside the property setter:
private double _logoScaleValue;
public double LogoScaleValue
{
get
{
return _logoScaleValue;
}
set
{
if(_logoScaleValue != value)
{
_logoScaleValue = value;
SetLogoSize(_logoScaleValue);
}
}
Upvotes: 1
Reputation: 1675
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)
{
}
}
Upvotes: 1