2016-10-03 26 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); 
     } 
    }