2012-07-26 36 views
1

我想让滑块在用户停止拖动时返回到0。滑块应该归零

到目前为止,我有这样的:

<Window x:Class="CenteredSliderTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<DockPanel> 
    <!--Value="{Binding ZSpeed}"--> 
    <Slider DockPanel.Dock="Left" 
           x:Name="ZSlider" 
           Minimum="-100" Maximum="100" 
           SelectionStart="-20" SelectionEnd="20" 
           Orientation="Vertical" 
           TickFrequency="10" 
           TickPlacement="TopLeft" 
           AutoToolTipPlacement="TopLeft" 
           AutoToolTipPrecision="2" 
           LargeChange="10" 
           SmallChange="1" 
           IsDirectionReversed="True" 
           Focusable="False" 
           > 
     <Slider.Triggers> 
      <EventTrigger RoutedEvent="LostMouseCapture" SourceName="ZSlider"> 
       <EventTrigger.Actions> 
        <BeginStoryboard> 
         <Storyboard> 
          <DoubleAnimation 
           Storyboard.TargetName="ZSlider" 
           Storyboard.TargetProperty="Value" 
           From="{Binding Value, ElementName=ZSlider}" 
           To="0.0" 
           Duration="0:0:1.5" 
           FillBehavior="Stop" 
           /> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger.Actions> 

      </EventTrigger> 
     </Slider.Triggers> 
    </Slider> 
    <TextBlock Text="{Binding ZSpeed}" /> 
</DockPanel> 
</Window> 

这个工作,只要我不绑定滑块值我的DependencyProperty ZSpeed。

只要我这样做,滑块将跳回原始值,并在第二次尝试滑块不能被拖动了。

所以我能做些什么(最好在XAML)为了让动画不仅修改滑块,而且ZSpeed属性?

编辑

代码在主窗口:

public partial class MainWindow : Window 
{ 


    public double ZSpeed 
    { 
     get { return (double)GetValue(ZSpeedProperty); } 
     set { SetValue(ZSpeedProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for ZSpeed. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ZSpeedProperty = 
     DependencyProperty.Register("ZSpeed", typeof(double), typeof(MainWindow), new UIPropertyMetadata(0.0)); 



    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = this; 

     Binding binding = new Binding("Value") { Source = ZSlider }; 
     this.SetBinding(ZSpeedProperty, binding); 
    } 

} 
+0

确实在绑定帮助中指定了'OneWay'? – paul 2012-07-26 12:25:22

+0

是的,但是然后ZSpeed没有相应设置。 – MTR 2012-07-26 12:32:45

+0

你为什么不把你的语言绑定到'ZSpeed'?滑块将自动更新。免责声明:我从来没有使用动画,所以我不知道这是否会起作用。 – 2012-07-26 12:33:01

回答

1

你可能会扭转结合的方向。您可以将ZSpeed绑定到Value,而不是绑定Slider的ValueZSpeed。如果滑块意图改变ZSpeed,这也将是“自然”绑定方向,但ZSpeed不会另外改变。

编辑:如果ZSpeed在一些数据类依赖属性MyData你可以创建一个在像这样的代码绑定:

MyData dataObject = ... 
Binding binding = new Binding("Value") { Source = ZSlider }; 
dataObject.SetBinding(MyData.ZSpeedProperty, binding); 

第二个编辑:拿起丹尼尔斯暗示,你可能动画ZSpeed代替滑块的Value。像以前一样绑定ValueZSpeed,除去EventTrigger并添加事件处理程序LostMouseCapture

<Slider x:Name="ZSlider" ... 
     Value="{Binding ZSpeed}" 
     LostMouseCapture="ZSlider_LostMouseCapture"/> 

后面的代码:

private void ZSlider_LostMouseCapture(object sender, MouseEventArgs e) 
{ 
    DoubleAnimation animation = new DoubleAnimation 
    { 
     From = ZSpeed, 
     To = 0d, 
     Duration = TimeSpan.FromSeconds(1.5 * Math.Abs(ZSpeed)/100d), 
     FillBehavior = FillBehavior.Stop 
    }; 
    ZSpeed = 0d; 
    BeginAnimation(ZSpeedProperty, animation); 
} 
+0

ZSpeed是我的视图模型的DependencyProperty,我该怎么做?我不知道如何“扭转绑定的方向”。顺便说一下,ZSpeed可以改变。 – MTR 2012-07-26 13:21:54

+0

查看编辑答案。 – Clemens 2012-07-26 13:26:17

+0

完成后:Binding binding = new Binding(“Value”){Source = ZSlider}; this.SetBinding(ZSpeedProperty,binding); ZSpeed动画为0,然后跳回原始值。动画运行时,滑块不会移动。 – MTR 2012-07-26 13:37:58

1

您应该使用FillBehavior.HoldEnd

编辑:这显然不起作用。您可以在StoryBoard.Completed事件中手动将ZSpeed值设置为0。

+0

HoldEnd在动画结束后冻结滑块。 – MTR 2012-07-26 13:23:04