我有一个像这样的滑块:
<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>
和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)
{
}
}
当我拖动滑块时,一切都很完美。但是,当我单击滑块时,拇指会捕捉到正确的位置,并且“SetLogoSize(Double?argument)&#39;得到了呼唤但是“#39;争论&#39;在跳转到这个新位置之前,它具有滑块拇指的先前值。这会导致滑块返回的值滞后于拇指的当前位置。
知道如何解决这个问题吗?
答案 0 :(得分:1)
使用单例模式,并在设置LogoScaleValue时仅调用SetLogoSize(Double? argument)
方法。
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)
{
}
}
答案 1 :(得分:1)
为什么要有命令?如果你在属性设置器中处理它会简单得多:
private double _logoScaleValue;
public double LogoScaleValue
{
get
{
return _logoScaleValue;
}
set
{
if(_logoScaleValue != value)
{
_logoScaleValue = value;
SetLogoSize(_logoScaleValue);
}
}