Я новичок в WPF. Я реализовал полноэкранное приложение с сетками, которые можно перемещать мышью (стиль drag'n'drop). Если элемент управления сетки выходит за границы экрана, он возвращается к своему состоянию по умолчанию с простой анимацией. Проблема в том, что когда сетка возвращается, она может быть перемещена больше! Предоставлены некоторые фрагменты кода:
public partial class MenuCard : UserControl, ITouchObject, INotifyPropertyChanged
{
...
public static readonly DependencyProperty XProperty =
DependencyProperty.Register("X", typeof(double), typeof(MenuCard), new UIPropertyMetadata(0.0, OnPosXChanged, CourceXValue));
...
private static void OnPosXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var owner = (MenuCard) d;
Grid rootGrid = (Grid)Application.Current.MainWindow.FindName("rootGrid");
Point actual = owner.TransformToAncestor(rootGrid).Transform(new Point(owner.X, owner.Y));
Point topLeft = owner.TransformToAncestor(rootGrid).Transform(new Point(0, 0));
Point bottomRight = owner.TransformToAncestor(rootGrid).Transform(new Point(owner.ActualWidth, owner.ActualHeight));
if (actual.X + (bottomRight.X - topLeft.X) <= border || actual.X >= SystemParameters.PrimaryScreenWidth - border)
{
ReturnToDefault(owner);
}
}
...
private static void ReturnToDefault(MenuCard owner)
{
DoubleAnimation yAnimation = new DoubleAnimation();
yAnimation.From = owner.Y;
yAnimation.DecelerationRatio = 0.5;
yAnimation.To = (double)YProperty.DefaultMetadata.DefaultValue;
owner.BeginAnimation(MenuCard.YProperty, yAnimation);
DoubleAnimation xAnimation = new DoubleAnimation();
xAnimation.From = owner.X;
xAnimation.DecelerationRatio = 0.5;
xAnimation.To = (double)XProperty.DefaultMetadata.DefaultValue;
owner.BeginAnimation(MenuCard.XProperty, xAnimation);
}
}
Основной класс:
public partial class MainWindow : Window
{
private void CanvasManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
var p = GetSender(e.ManipulationContainer as FrameworkElement);
if (p != null)
{
ManipulationDelta md = e.DeltaManipulation;
p.X += md.Translation.X;
p.Y += md.Translation.Y;
p.Angle += md.Rotation;
p.ScaleX *= md.Scale.X;
p.ScaleY *= md.Scale.Y;
}
e.Handled = true;
}
private ITouchObject GetSender(FrameworkElement element)
{
while (true)
{
if (element.Parent == null)
{
return null;
}
if (element is ITouchObject)
{
return element as ITouchObject;
}
element = element.Parent as FrameworkElement;
}
}
}
У меня нет идей. Любая помощь будет оценена!