Benutzer:MovGP0/WPF/Behaviors
< Benutzer:MovGP0 | WPF
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Custom Behavior[Bearbeiten | Quelltext bearbeiten]
[TypeConstraint(typeof(UIElement))]
[ContentProperty(Name="Actions")]
public sealed class SwipeTriggerBehavior : DependencyObject, IBehavior
{
#region Actions
public static readonly DependencyProperty ActionsProperty = DependencyProperty.Register(nameof(Actions), typeof(ActionCollection), typeof(SwipeTriggerBehavior), new PropertyMetadata(null));
public ActionCollection Actions
{
get
{
return (ActionCollection)GetValue(ActionsProperty);
}
set
{
SetValue(ActionsProperty, value);
}
}
#endregion
#region Direction
public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register(nameof(Direction), typeof(SwipeDirection), typeof(SwipeTriggerBehavior), new PropertyMetadata(null));
public SwipeDirection Direction
{
get
{
return (SwipeDirection)GetValue(ActionsProperty);
}
set
{
SetValue(ActionsProperty, value);
}
}
#endregion
#region IBehavior
public DependencyObject AssociatedObject { get; private set; }
public void Attach(DependencyObject associatedObject)
{
if (AssociatedObject == associatedObject || DesignMode.DesignModeEnabled)
return;
AssociatedObject = associatedObject;
var obj = AssociatedObject as UIElement;
if (obj != null)
{
obj.ManipulationCompleted += OnManipulationCompleted;
}
}
public void Detach()
{
var obj = AssociatedObject as UIElement;
if (obj != null)
{
obj.ManipulationCompleted -= OnManipulationCompleted;
}
}
#endregion
private void OnManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
if(MatchesDirection(e.Velocities.Linear, Direction))
Execute(AssociatedObject, null);
}
private static bool MatchesDirection(Point e, SwipeDirection direction)
{
var isRight = e.X.Between(0.3, 100);
var isLeft = e.X.Between(-100, -0.3);
var isUp = e.Y.Between(-100, -0.3);
var isDown = e.Y.Between(0.3, 100);
switch (direction)
{
case SwipeDirection.Left:
return isLeft && !(isUp || isDown);
case SwipeDirection.Right:
return isRight && !(isUp || isDown);
case SwipeDirection.Up:
return isUp && !(isRight || isLeft);
case SwipeDirection.Down:
return isDown && !(isRight || isLeft);
case SwipeDirection.LeftDown:
return isLeft && isDown;
case SwipeDirection.LeftUp:
return isLeft && isUp;
case SwipeDirection.RightDown:
return isRight && isDown;
case SwipeDirection.RightUp:
return isRight && isUp;
default:
throw new ArgumentOutOfRangeException();
}
}
private void Execute(object sender, object parameter)
{
var actions = Actions;
if (actions != null)
{
Interaction.ExecuteActions(sender, actions, parameter);
}
}
}
<Grid>
<interactivity:Interaction.Behaviors>
<behaviors:SwipeTriggerBehavior Direction="Up">
<core:ChangePropertyAction PropertyName="Text" TargetObject="{Binding ElementName=myTextBox}" Value="Up" />
</behaviors:SwipeTriggerBehavior>
<interactivity:Interaction.Behaviors>
<Grid>
References[Bearbeiten | Quelltext bearbeiten]
Weblinks[Bearbeiten | Quelltext bearbeiten]
|