private void AssociatedObject_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) { if (PreviewBehavior._isShowingPreview) { Point point = new Point(e.ManipulationOrigin.X, e.ManipulationOrigin.Y); UIElement rootVisual = Application.Current.RootVisual; FrameworkElement newHoveredElement = VisualTreeHelper.FindElementsInHostCoordinates(PreviewBehavior.GetHostCoordinates(e.ManipulationContainer.TransformToVisual(rootVisual).Transform(point)), rootVisual).FirstOrDefault <UIElement>() as FrameworkElement; if (newHoveredElement != null) { BehaviorCollection behaviors1 = Interaction.GetBehaviors((DependencyObject)newHoveredElement); PreviewBehavior previewBehavior = (behaviors1 != null ? behaviors1.FirstOrDefault <Behavior>((Func <Behavior, bool>)(b => b is PreviewBehavior)) : (Behavior)null) as PreviewBehavior; if (previewBehavior == null && newHoveredElement.Parent != null) { BehaviorCollection behaviors2 = Interaction.GetBehaviors(newHoveredElement.Parent); previewBehavior = (behaviors2 != null ? behaviors2.FirstOrDefault <Behavior>((Func <Behavior, bool>)(b => b is PreviewBehavior)) : (Behavior)null) as PreviewBehavior; } if (previewBehavior != null) { Image image = newHoveredElement as Image; BitmapImage originalImage = (image != null ? image.Source : null) as BitmapImage; this.ShowPreview(previewBehavior.PreviewUri, originalImage, previewBehavior.TopOffset); this.SetHoveredElement(newHoveredElement); } } e.Handled = true; } else { this.StopTimer(); } }
static void OnEnableAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Window w = d as Window; BehaviorCollection col = Interaction.GetBehaviors(w); WindowFadeAnimationBehavior b = (WindowFadeAnimationBehavior)col.FirstOrDefault(x => x is WindowFadeAnimationBehavior); col.Remove(b); if ((bool)e.NewValue) { col.Add(new WindowFadeAnimationBehavior()); } }
public static void AttachTo(DependencyObject obj) { if (!(obj is FrameworkElement || obj is FrameworkContentElement)) { return; } BehaviorCollection bCol = Interaction.GetBehaviors(obj); ParameterAndParentViewModelSyncBehavior b = (ParameterAndParentViewModelSyncBehavior)bCol.FirstOrDefault(x => x is ParameterAndParentViewModelSyncBehavior); if (b != null) { return; } bCol.Add(new ParameterAndParentViewModelSyncBehavior()); }
/// <summary> /// This adds the behavior to an image through an attached property. /// </summary> /// <param name="dpo"></param> /// <param name="e"></param> private static void OnIsActiveChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e) { BehaviorCollection bc = Interaction.GetBehaviors(dpo); if (Convert.ToBoolean(e.NewValue)) { bc.Add(new AutoDisabledImageBehavior()); } else { var behavior = bc.FirstOrDefault(beh => beh.GetType() == typeof(AutoDisabledImageBehavior)); if (behavior != null) { bc.Remove(behavior); } } }