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); 
    }

}
4

2 回答 2

1

你应该使用FillBehavior.HoldEnd.

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

于 2012-07-26T13:00:06.270 回答
1

您可能会反转绑定的方向。而不是将 Slider绑定ValueZSpeed您可以绑定ZSpeedValue. 如果 Slider 要更改,这也是“自然”绑定方向ZSpeed,但ZSpeed不会更改。

编辑:如果ZSpeed是某些数据类中的依赖属性,MyData您可以在代码中创建一个绑定,如下所示:

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

第二次编辑:采纳 Daniels 的建议,您可能会制作动画ZSpeed而不是 Slider 的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);
}
于 2012-07-26T12:57:17.360 回答