NOVINKA! E-learningové kurzy umělé inteligence. Nyní AI za nejlepší ceny. Zjisti více:
NOVINKA – Víkendový online kurz Software tester, který tě posune dál. Zjisti, jak na to!
Avatar
ORRNY99
Člen
Avatar
ORRNY99:10.8.2015 17:16

Dobrý den,
neví někdo jak by se dala udělat ve wpf animovaná změna pozice slideru (thumb) něco podobného jako je na obr http://i.imgur.com/AxuZr52.gifv
tady když tak je zdroják mého slideru. Díky moc

<Slider x:Name="timelineSlider" Background="{x:Null}" BorderBrush="{x:Null}" IsMoveToPointEnabled="True" TickPlacement="None" SnapsToDevicePixels="False" OpacityMask="{x:Null}" IsManipulationEnabled="True" IsSnapToTickEnabled="False" IsSelectionRangeEnabled="False" IsTabStop="False" AutoToolTipPlacement="TopLeft" AllowDrop="False" Height="16" Margin="31,4,26,0" VerticalAlignment="Top" Grid.ColumnSpan="3">
            <Slider.Resources>
                <Color x:Key="Background">Black</Color>
                <Color x:Key="Foreground">Black</Color>
                <SolidColorBrush x:Key="ColorBackground" Color="{StaticResource ResourceKey=Background}" />
                <SolidColorBrush x:Key="ColorForeground" Color="{StaticResource ResourceKey=Foreground}" />
                <SolidColorBrush x:Key="ColorDisabled" Color="Gray" />
                <!-- DECREASE BUTTON STYLE -->
                <Style x:Key="BackwardSliderButtonStyle" TargetType="{x:Type RepeatButton}">
                    <Setter Property="SnapsToDevicePixels" Value="true" />
                    <Setter Property="OverridesDefaultStyle" Value="true" />
                    <Setter Property="IsTabStop" Value="false" />
                    <Setter Property="Focusable" Value="false" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type RepeatButton}">
                                <Border Background="#e82a2c" BorderBrush="Transparent" BorderThickness="0,0,0,0" Height="4" CornerRadius="2,0,0,2" Opacity="100" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <!-- INCREASE BUTTON STYLE -->
                <Style x:Key="ForwardSliderButtonStyle" TargetType="{x:Type RepeatButton}">
                    <Setter Property="SnapsToDevicePixels" Value="true" />
                    <Setter Property="OverridesDefaultStyle" Value="true" />
                    <Setter Property="IsTabStop" Value="false" />
                    <Setter Property="Focusable" Value="false" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type RepeatButton}">
                                <Border Background="#762C2C2C" BorderBrush="Transparent" BorderThickness="0,0,0,0" Height="4" CornerRadius="0,2,2,0" Opacity="100" />
                            </ControlTemplate>
                      </Setter.Value>
                    </Setter>
                </Style>
                <!-- THUMB STYLE -->
                <Style x:Key="SliderThumbStyle" TargetType="{x:Type Thumb}">
                    <Setter Property="SnapsToDevicePixels" Value="true" />
                    <Setter Property="OverridesDefaultStyle" Value="true" />
                    <Setter Property="Height" Value="15" />
                    <Setter Property="Width" Value="15" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Thumb}">
                                <Ellipse x:Name="normal" Stroke="#e82a2c" StrokeThickness="3" Fill="#762C2C2C" Opacity="1" />
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <!--<Setter TargetName="thumb" Property="Background" Value="Gray"/>
                                        <Setter TargetName="thumb" Property="BorderBrush" Value="White"/>
                                        <Setter TargetName="thumb" Property="BorderThickness" Value="0"/>-->
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <Style TargetType="{x:Type Slider}">
                    <Setter Property="SnapsToDevicePixels" Value="true" />
                    <Setter Property="OverridesDefaultStyle" Value="true" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Slider}">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <TickBar x:Name="TopTick" SnapsToDevicePixels="True" Placement="Top" Fill="Orange" Height="4" Visibility="Collapsed" />
                                    <Rectangle StrokeThickness="0" Fill="Black" Stroke="Black" Grid.Row="1" Height="10" RadiusX="1" RadiusY="1">
                                        <Rectangle.Effect>
                                            <BlurEffect Radius="10" />
                                        </Rectangle.Effect>
                                    </Rectangle>
                                    <Border x:Name="TrackBackground" Grid.Row="1" Background="Transparent" BorderBrush="Black" BorderThickness="0" Height="7" />
                                    <Track Grid.Row="1" x:Name="PART_Track">
                                        <Track.DecreaseRepeatButton>
                                            <RepeatButton Style="{StaticResource BackwardSliderButtonStyle}" Command="Slider.DecreaseLarge" />
                                        </Track.DecreaseRepeatButton>
                                        <Track.Thumb>
                                            <Thumb Style="{StaticResource SliderThumbStyle}" />
                                        </Track.Thumb>
                                        <Track.IncreaseRepeatButton>
                                            <RepeatButton Style="{StaticResource ForwardSliderButtonStyle}" Command="Slider.IncreaseLarge" />
                                        </Track.IncreaseRepeatButton>
                                    </Track>
                                    <TickBar x:Name="BottomTick" SnapsToDevicePixels="True" Grid.Row="2" Fill="White" Placement="Bottom" Height="4" Visibility="Collapsed" />
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="TickPlacement" Value="TopLeft">
                                        <Setter TargetName="TopTick" Property="Visibility" Value="Visible" />
                                    </Trigger>
                                    <Trigger Property="TickPlacement" Value="BottomRight">
                                        <Setter TargetName="BottomTick" Property="Visibility" Value="Visible" />
                                    </Trigger>
                                    <Trigger Property="TickPlacement" Value="Both">
                                        <Setter TargetName="TopTick" Property="Visibility" Value="Visible" />
                                        <Setter TargetName="BottomTick" Property="Visibility" Value="Visible" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Slider.Resources>
        </Slider>
 
Odpovědět
10.8.2015 17:16
Avatar
Odpovídá na ORRNY99
sadlomaslox25:10.8.2015 21:05

zatim me nic lepsiho nez toto nenapadlo. staci predelat xaml kod aby odkazoval na toto.

public class MySlider : Slider
{
    private bool Animating;
    static MySlider()
    {
        ValueProperty.OverrideMetadata(typeof(MySlider), new FrameworkPropertyMetadata(0.0, (a, b) =>
         {
             var slider = a as MySlider;
             var from = (double)b.OldValue;
             var to = (double)b.NewValue;
             if (Math.Abs(from - to) < 0.2 || slider.Animating)
                 return;
             slider.Animating = true;

             var animation = new DoubleAnimation((double)b.OldValue, (double)b.NewValue, new Duration(TimeSpan.FromSeconds(0.3)));
             animation.EasingFunction = new CubicEase();
             var clock = animation.CreateClock();

             slider.ApplyAnimationClock(ValueProperty, clock);
             clock.Completed += (xa, bx) => slider.Animating = false;
         }));
    }
}
 
Nahoru Odpovědět
10.8.2015 21:05
Avatar
ORRNY99
Člen
Avatar
ORRNY99:11.8.2015 19:35

Tak jsem to nakonec nějak udělal. Jinak díky za radu

public class MySlider : Slider
   {
       private Track _track;
       public override void OnApplyTemplate()
       {
           base.OnApplyTemplate();
           _track = GetTemplateChild("PART_Track") as Track;
       }

       protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
       {
           if (IsMoveToPointEnabled && _track != null && _track.Thumb != null && !_track.Thumb.IsMouseOver)
           {

               Point pt = e.MouseDevice.GetPosition(_track);

               double newValue = _track.ValueFromPoint(pt);
               if (!double.IsInfinity(newValue))
                   UpdateValue(newValue);
               e.Handled = true;
           }
       }

       private void UpdateValue(double value)
       {
           Double snappedValue = SnapToTick(value);

           if (snappedValue != Value)
           {
               DoubleAnimation animation = new DoubleAnimation();
               //animation.From = value;
               animation.To = Math.Max(this.Minimum, Math.Min(this.Maximum, snappedValue));
               animation.Duration = TimeSpan.FromSeconds(0.2);
               Storyboard.SetTargetProperty(animation, new PropertyPath("Value"));
               Storyboard.SetTarget(animation, this);

               Storyboard sb = new Storyboard();
               sb.Children.Add(animation);
               sb.Begin();
           }
       }

       private double SnapToTick(double value)
       {
           if (IsSnapToTickEnabled)
           {
               double previous = Minimum;
               double next = Maximum;

               if (TickFrequency > 0.0)
               {
                   previous = Minimum + (Math.Round(((value - Minimum) / TickFrequency)) * TickFrequency);
                   next = Math.Min(Maximum, previous + TickFrequency);
               }

               value = (value > ((previous + next) * 0.2)) ? next : previous;
           }

           return value;
       }
   }
 
Nahoru Odpovědět
11.8.2015 19:35
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 3 zpráv z 3.