2016-10-03 29 views
2

我有一个这样的滑块:滑块的值落在了其拇指的当前位置

<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> 

和视图模型:

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?参数)'被调用,但'参数'具有滑块大拇指的前一个值,然后跳到这个新位置。这会导致滑块返回的值落后于拇指的当前位置。

任何想法如何解决这个问题?

+1

您是否尝试过刚刚访问'LogoScaleValue'在'SetLogoSize()'方法代替人体传递作为参数的?它绑定到滑块值,因此它应该具有最新值。 –

+0

谢谢,这完全奏效! – Ivan

回答

1

使用Singleton模式,只有当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

为什么要命令呢?这是更简单,如果你处理它的属性设置里面:

private double _logoScaleValue; 

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