Diskuze: wpf slider animation thumb
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Zobrazeno 3 zpráv z 3.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
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;
}));
}
}
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;
}
}
Zobrazeno 3 zpráv z 3.