public bool?Show(object dataContext, string caption) { ContentControl contentControl = new ContentControl(); contentControl.Template = DialogControlTemplate; Window window = new Window(); window.ResizeMode = ResizeMode.NoResize; window.Owner = Application.Current.MainWindow; window.WindowStartupLocation = WindowStartupLocation.CenterOwner; window.SizeToContent = SizeToContent.WidthAndHeight; window.DataContext = dataContext; window.Content = contentControl; window.Title = caption; DialogHostBehavior behavior = new DialogHostBehavior(); behavior.DialogControlTemplate = DialogControlTemplate; BehaviorCollection behaviors = Interaction.GetBehaviors(window); behaviors.Add(behavior); return(window.ShowDialog()); }
void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { this.AssociatedObject.Loaded -= new RoutedEventHandler(AssociatedObject_Loaded); BehaviorCollection parent_attached_behaviors = Interaction.GetBehaviors(this.AssociatedObject.Parent); ResizeBehavior parent_resize_behavior = null; foreach (Behavior b in parent_attached_behaviors) { if (b.GetType() == typeof(ResizeBehavior)) { parent_resize_behavior = (ResizeBehavior)b; break; } } if (parent_resize_behavior == null) { (this.AssociatedObject.Parent as FrameworkElement).SizeChanged += new SizeChangedEventHandler(ParentSizeChanged); } else { //attach to parent's resize behavior to notice size changes parent_resize_behavior.SizeChanged += new EventHandler(ParentBehaviorSizeChanged); } }
private static int GetIndexOf(BehaviorCollection itemBehaviors, Behavior behavior) { var index = -1; var orignalBehavior = GetOriginalBehavior(behavior); for (var i = 0; i < itemBehaviors.Count; i++) { Behavior currentBehavior = itemBehaviors[i]; if (Equals(currentBehavior, behavior) || Equals(currentBehavior, orignalBehavior)) { index = i; break; } Behavior currentOrignalBehavior = GetOriginalBehavior(currentBehavior); if (Equals(currentOrignalBehavior, behavior) || Equals(currentOrignalBehavior, orignalBehavior)) { index = i; break; } } return index; }
/// <summary> /// Gets the <see cref="BehaviorCollection"/> associated with a specified object. /// </summary> /// <param name="obj">The object from which to retrieve the <see cref="BehaviorCollection"/>.</param> /// <returns>A <see cref="BehaviorCollection"/> containing the behaviors associated with the specified object.</returns> public static BehaviorCollection GetBehaviors(DependencyObject obj) { var behaviorCollection = (BehaviorCollection)obj.GetValue(BehaviorsProperty); if (behaviorCollection == null) { #if WINDOWS_PHONE || WINDOWS_PHONE_81 behaviorCollection = (BehaviorCollection)typeof(BehaviorCollection) .GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null) .Invoke(null); #else behaviorCollection = new BehaviorCollection(); #endif obj.SetValue(BehaviorsProperty, behaviorCollection); obj.SetValue(Interaction.BehaviorsProperty, behaviorCollection); var frameworkElement = obj as FrameworkElement; if (frameworkElement != null) { frameworkElement.Loaded -= FrameworkElement_Loaded; frameworkElement.Loaded += FrameworkElement_Loaded; frameworkElement.Unloaded -= FrameworkElement_Unloaded; frameworkElement.Unloaded += FrameworkElement_Unloaded; } } return(behaviorCollection); }
private static int GetIndexOf(BehaviorCollection itemBehaviors, Behavior behavior) { int index = -1; Behavior orignalBehavior = GetOriginalBehavior(behavior); for (int i = 0; i < itemBehaviors.Count; i++) { Behavior currentBehavior = itemBehaviors[i]; if (currentBehavior == behavior || currentBehavior == orignalBehavior) { index = i; break; } Behavior currentOrignalBehavior = GetOriginalBehavior(currentBehavior); if (currentOrignalBehavior == behavior || currentOrignalBehavior == orignalBehavior) { index = i; break; } } return(index); }
/// <summary> /// Properties the name changed. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <param name="arg">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void PropertyNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs arg) { var control = (Control)dependencyObject; var collection = new BehaviorCollection(); var validationBehavior = new ValidationBehavior() { PropertyName = arg.NewValue.ToString() }; var changePropertyIsValidAction = new ChangePropertyAction { PropertyName = new PropertyPath(nameof(Control.Style)), Value = null }; var errorStyle = new Style { TargetType = typeof(Control) }; errorStyle.Setters.Add(new Setter(Control.BorderBrushProperty, new SolidColorBrush(Colors.Red))); var changePropertyIsInvalidAction = new ChangePropertyAction { PropertyName = new PropertyPath(nameof(Control.Style)), Value = errorStyle }; validationBehavior.WhenValidActions.Add(changePropertyIsValidAction); validationBehavior.WhenInvalidActions.Add(changePropertyIsInvalidAction); collection.Add(validationBehavior); control.SetValue(Interaction.BehaviorsProperty, collection); }
public void Initialize() { ExcelLikeUiGridExcelMarkerMouseController controller = new ExcelLikeUiGridExcelMarkerMouseController(this.grid1); ExcelLikeUiGridExcelMarkerMouseController controller2 = new ExcelLikeUiGridExcelMarkerMouseController(this.grid2); ExcelLikeUiGridExcelMarkerMouseController controller3 = new ExcelLikeUiGridExcelMarkerMouseController(this.grid3); this.grid1.Model.CommandStack.Enabled = true; this.grid2.Model.CommandStack.Enabled = true; this.grid3.Model.CommandStack.Enabled = true; this.grid1.MouseControllerDispatcher.Add(controller); this.grid2.MouseControllerDispatcher.Add(controller2); this.grid3.MouseControllerDispatcher.Add(controller3); this.grid1.Model.Options.CopyPasteOption = CopyPaste.CutCell | CopyPaste.CopyCellData | CopyPaste.PasteCell | CopyPaste.IncludeStyle; this.grid2.Model.Options.CopyPasteOption = CopyPaste.CutCell | CopyPaste.CopyCellData | CopyPaste.PasteCell | CopyPaste.IncludeStyle; this.grid3.Model.Options.CopyPasteOption = CopyPaste.CutCell | CopyPaste.CopyCellData | CopyPaste.PasteCell | CopyPaste.IncludeStyle; GridTextBoxBinder binder = new GridTextBoxBinder(); binder.Wire(new GridControlBase[] { grid1, grid2, grid3 }, Formulacell); BehaviorCollection grid1Behaviors = Interaction.GetBehaviors(grid1); BehaviorCollection grid2Behaviors = Interaction.GetBehaviors(grid2); BehaviorCollection grid3Behaviors = Interaction.GetBehaviors(grid3); grid1Behaviors.Add(new ExcelLikeUiCurrentCellSyncBehavior()); grid2Behaviors.Add(new ExcelLikeUiCurrentCellSyncBehavior()); grid3Behaviors.Add(new ExcelLikeUiCurrentCellSyncBehavior()); grid1Behaviors.Add(new CellLocationSyncBehavior()); grid2Behaviors.Add(new CellLocationSyncBehavior()); grid3Behaviors.Add(new CellLocationSyncBehavior()); }
public void AttachBehaviorToConstrainedCollection_ShouldThrow() { // verify we can't host a constrained Behavior on the wrong type StubRectangleBehavior rectangleBehavior = new StubRectangleBehavior(); Rectangle rectangle1 = new Rectangle(); Button button4 = new Button(); BehaviorCollection rectangleBehaviors = Interaction.GetBehaviors(rectangle1); BehaviorCollection buttonBehaviors = Interaction.GetBehaviors(button4); rectangleBehaviors.Add(rectangleBehavior); Assert.AreEqual(rectangleBehaviors.Count, 1, "After attaching RectangleBehavior to rectangleBehaviors, rectangleBehaviors.Count should be 1"); Assert.AreEqual(((IAttachedObject)rectangleBehavior).AssociatedObject, rectangle1, "rectangleBehavior.AssociatedObject == rectangle"); rectangleBehaviors.Remove(rectangleBehavior); Assert.AreEqual(rectangleBehaviors.Count, 0, "After detaching rectangleBehavior, rectangleBehaviors.Count should be 0"); Assert.IsNull(((IAttachedObject)rectangleBehavior).AssociatedObject, "rectangleBehavior.AssociatedObject == null"); try { buttonBehaviors.Add(rectangleBehavior); Assert.Fail("Expected InvalidOperationException to be thrown thrown."); } catch (InvalidOperationException) { } }
private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e) { BehaviorCollection collection = null; var template = (DataTemplate)e.NewValue; if (template != null) { var value = template.LoadContent(); if (value is BehaviorCollection) { collection = (BehaviorCollection)value; } else if (value is IBehavior) { collection = new BehaviorCollection { value } } ; else { throw new Exception($"AttachedBehaviors should be a BehaviorCollection or an IBehavior."); } } // collection may be null here, if e.NewValue is null Interaction.SetBehaviors(d, collection); }
private void LayoutRootManipulationStarted(object sender, ManipulationStartedEventArgs e) { BehaviorCollection behaviorCollection = Interaction.GetBehaviors(_finger); if (behaviorCollection.Count == 0) { behaviorCollection.Add(new PhysicsObjectBehavior { BoundaryElement = "finger", IsStatic = true, IsSensor = true }); _physicsController.AddPhysicsBody( _finger.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain); } PhysicsSprite finger = _physicsController.PhysicsObjects["finger"]; finger.Collision += FingerCollision; finger.BodyObject.OnSeparation += BodyObject_OnSeparation; //finger phys object already created if (finger != null) { finger.Position = new Vector2((float)(e.ManipulationOrigin.X), (float)(e.ManipulationOrigin.Y)); } e.Handled = true; }
private static void OnTemplateChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { InteractivityTemplate dt = (InteractivityTemplate)e.NewValue; if (dt == null) { return; } #if (!SILVERLIGHT) dt.Seal(); #endif InteractivityItems ih = (InteractivityItems)dt.LoadContent(); BehaviorCollection bc = Interaction.GetBehaviors(d); Microsoft.Xaml.Behaviors.TriggerCollection tc = Interaction.GetTriggers(d); foreach (Behavior behavior in ih.Behaviors) { bc.Add(behavior); } foreach (TriggerBase trigger in ih.Triggers) { tc.Add(trigger); } }
/// <summary> /// Shows the specified window as a notification. /// </summary> /// <param name="window">The window.</param> /// <param name="displayDuration">The display duration.</param> public static void Show(Window window, TimeSpan displayDuration) { BehaviorCollection behaviors = Interaction.GetBehaviors(window); behaviors.Add(new FadeBehavior()); behaviors.Add(new SlideBehavior()); window.Top = SystemParameters.WorkArea.Height - window.Height - window.Margin.Top; window.Left = SystemParameters.WorkArea.Width - window.Width - window.Margin.Right - Margin; notificationWindowsCount += 1; WindowInfo windowInfo = new WindowInfo() { ID = notificationWindowsCount, DisplayDuration = displayDuration, Window = window }; windowInfo.Window.Closed += Window_Closed; if (notificationWindows.Count + 1 > MAX_NOTIFICATIONS) { notificationsBuffer.Add(windowInfo); } else { Observable .Timer(displayDuration) .ObserveOnDispatcher() .Subscribe(x => OnTimerElapsed(windowInfo)); notificationWindows.Add(windowInfo); window.Show(); } }
private void removeMapBehavior(ExtensionBehavior extensionBehavior) { if (MapApplication.Current == null && MapApplication.Current.Map != null) { return; } BehaviorCollection behaviors = Interaction.GetBehaviors(MapApplication.Current.Map); if (behaviors == null) { return; } Behavior <Map> mapBehavior = extensionBehavior.MapBehavior as Behavior <Map>; if (mapBehavior == null) { mapBehavior = BehaviorUtils.GetMapBehavior(extensionBehavior); extensionBehavior.MapBehavior = mapBehavior; } if (mapBehavior == null) { return; } behaviors.Remove(mapBehavior); if (View.Instance != null && View.Instance.ExtensionBehaviors != null && View.Instance.ExtensionBehaviors.Contains(extensionBehavior)) { View.Instance.ExtensionBehaviors.Remove(extensionBehavior); } }
public static void SetBehaviors(DependencyObject obj, BehaviorCollection value) { if (obj == null) throw new ArgumentNullException("obj"); obj.SetValue(BehaviorsProperty, value); }
private static int GetIndexOf(BehaviorCollection itemBehaviors, Behavior behavior) { int index = -1; Behavior orignalBehavior = GetOriginalBehavior(behavior); for (int i = 0; i < itemBehaviors.Count; i++) { Behavior currentBehavior = itemBehaviors[i]; if (currentBehavior == behavior || currentBehavior == orignalBehavior) { index = i; break; } Behavior currentOrignalBehavior = GetOriginalBehavior(currentBehavior); if (currentOrignalBehavior == behavior || currentOrignalBehavior == orignalBehavior) { index = i; break; } } return index; }
public void SetBehaviors_ManualDetachThenNull_DoesNotDoubleDetach() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); Button button = new Button(); Interaction.SetBehaviors(button, behaviorCollection); foreach (StubBehavior behavior in behaviorCollection) { behavior.Detach(); } Interaction.SetBehaviors(button, null); foreach (StubBehavior behavior in behaviorCollection) { Assert.AreEqual(1, behavior.DetachCount, "Setting BehaviorCollection to null should not call Detach on already Detached Behaviors."); Assert.IsNull(behavior.AssociatedObject, "AssociatedObject should be null after Detach."); } }
/// <summary> /// Shows the specified window as a notification. /// </summary> /// <param name="window">The window.</param> /// <param name="displayDuration">The display duration.</param> public static void Show(Window window, TimeSpan displayDuration, NotificationFlowDirection notificationFlowDirection) { BehaviorCollection behaviors = Interaction.GetBehaviors(window); behaviors.Add(new FadeBehavior()); behaviors.Add(new SlideBehavior()); SetWindowDirection(window, notificationFlowDirection); notificationWindowsCount += 1; WindowInfo windowInfo = new WindowInfo() { ID = notificationWindowsCount, DisplayDuration = displayDuration, Window = window }; windowInfo.Window.Closed += Window_Closed; if (notificationWindows.Count + 1 > MAX_NOTIFICATIONS) { notificationsBuffer.Add(windowInfo); } else { StartWindowCloseTimer(windowInfo); window.Show(); notificationWindows.Add(windowInfo); } }
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(); } }
private void CarouselControl_Loaded(object sender, RoutedEventArgs e) { BehaviorCollection behaviors = Interaction.GetBehaviors(pathListBox); pathListBoxScrollBehavior = (PathListBoxScrollBehavior)behaviors .Where(x => x.GetType() == typeof(PathListBoxScrollBehavior)).FirstOrDefault(); }
private static int GetIndexOf(BehaviorCollection itemBehaviors, Behavior behavior) { int index = -1; Guid behaviorId = GetBehaviorId(behavior); for (int i = 0; i < itemBehaviors.Count; i++) { Behavior currentBehavior = itemBehaviors[i]; if (currentBehavior == behavior) { index = i; break; } Guid cloneId = GetBehaviorId(currentBehavior); if (cloneId == behaviorId) { index = i; break; } } return(index); }
/// <summary> /// Shows the specified window as a notification. /// </summary> /// <param name="window">The window.</param> /// <param name="displayDuration">The display duration.</param> public static void Show(Window window, TimeSpan displayDuration) { BehaviorCollection behaviors = Interaction.GetBehaviors(window); behaviors.Add(new FadeBehavior()); behaviors.Add(new SlideBehavior()); window.Top = SystemParameters.WorkArea.Height - window.Height - window.Margin.Top - windows.Sum(x => x.Window.Height - x.Window.Margin.Top + Margin); window.Left = SystemParameters.WorkArea.Width - window.Width - window.Margin.Right - Margin; WindowInfo windowInfo = new WindowInfo() { DisplayDuration = displayDuration, Window = window }; windows.Add(windowInfo); window.Show(); Observable .Timer(displayDuration) .ObserveOnDispatcher() .Subscribe(x => OnTimerElapsed(windowInfo)); }
/// <summary> /// The get fade behaviour. /// </summary> /// <param name="frameworkElement"> The framework element. </param> /// <param name="beginTime"> The begin time. </param> /// <param name="duration"> The duration. </param> /// <returns> The <see cref="FadeBehavior"/>. </returns> private static FadeBehavior GetFadeBehavior( FrameworkElement frameworkElement, TimeSpan beginTime, TimeSpan duration) { BehaviorCollection behaviors = Interaction.GetBehaviors(frameworkElement); FadeBehavior fadeBehavior = behaviors .OfType <FadeBehavior>() .FirstOrDefault(); if (fadeBehavior == null) { fadeBehavior = new FadeBehavior() { IsAnimatingOnIsVisibleChanged = false, IsAnimatingOnLoaded = false }; behaviors.Add(fadeBehavior); } fadeBehavior.BeginTime = beginTime; fadeBehavior.Duration = duration; return(fadeBehavior); }
public override void OnApplyTemplate() { base.OnApplyTemplate(); _viewBox = Template.FindName("PART_VIEWBOX", this) as FrameworkElement; if (IsManipulable) { _manipulable = new Manipulable() { Container = _parent }; TapBehavior _tapbehavior = new TapBehavior() { Threshold = 5 }; _manipulable.SupportedManipulations = ManipulationTypes.All; _manipulable.Manipulate += new EventHandler <ManipulationDeltaEventArgs>(_manipulable_Manipulate); _tapbehavior.Tap += new EventHandler <TapEventArgs>(_tapbehavior_DoubleTap); BehaviorCollection bCollection = Interaction.GetBehaviors(this); bCollection.Add(_manipulable); bCollection.Add(_tapbehavior); } }
/// <summary> /// Sets the name of the command associated with the specified Button. /// </summary> /// <param name="button">The Button to associate the command with.</param> /// <param name="commandName">The name of the command to associated.</param> public static void SetCommand(ButtonBase button, string commandName) { BehaviorCollection behaviors = GetBehaviors(button); foreach (Behavior behavior in behaviors) { Command commandBehavior = behavior as Command; if (commandBehavior != null) { if (String.IsNullOrEmpty(commandName)) { behaviors.Remove(commandBehavior); } else { commandBehavior.CommandName = commandName; } return; } } if (String.IsNullOrEmpty(commandName) == false) { Command commandBehavior = new Command(); commandBehavior.CommandName = commandName; behaviors.Add(commandBehavior); } }
/// <summary> /// Initializes a new instance of the <see cref="UIElement" /> class. /// </summary> /// <remarks> /// </remarks> protected UIElement() { string type = GetType().Name; if (!TypeCounter.ContainsKey(type)) { TypeCounter.Add(type, 1); } else { ++TypeCounter[type]; } Name = string.Format("{0}{1}", type, TypeCounter[type]); Children = new UIElementCollection(this); bindings = new Dictionary <string, BindingExpression>(); behaviors = new BehaviorCollection(); animator = new AnimationController(this); DependencyProperties = new PropertyContainer(this); width = float.NaN; height = float.NaN; depth = float.NaN; MinimumWidth = 4; MinimumHeight = 4; MinimumDepth = 0; MaximumWidth = float.PositiveInfinity; MaximumHeight = float.PositiveInfinity; MaximumDepth = float.PositiveInfinity; verticalAlignment = VerticalAlignment.Stretch; horizontalAlignment = HorizontalAlignment.Stretch; }
/// <summary> /// Raised when the action's target object changes. Removes the behavior from the previous target /// and adds it to the new one. /// </summary> protected override void OnTargetChanged(DependencyObject oldTarget, DependencyObject newTarget) { base.OnTargetChanged(oldTarget, newTarget); // Detach behavior from previous target if (oldTarget != null) { BehaviorCollection behaviors = Interaction.GetBehaviors(oldTarget); if (behaviors.Contains(Behavior)) { behaviors.Remove(Behavior); } } // Attach behavior to the new target, making sure that the new target is of the behavior's // target type. if (newTarget != null && isTargetTypeValid(newTarget, Behavior.GetType())) { BehaviorCollection behaviors = Interaction.GetBehaviors(newTarget); if (!behaviors.Contains(Behavior)) { behaviors.Add(Behavior); } } }
private void UpdateText() { if (Control == null || Element == null) { return; } if (string.IsNullOrEmpty(Control.Text)) { return; } // Gets the complete HTML string var helper = new LabelRendererHelper(Element, Element.Text); Control.Text = helper.ToString(); // Adds the HtmlTextBehavior because UWP's TextBlock // does not natively support HTML content var behavior = new HtmlTextBehavior((HtmlLabel)Element); BehaviorCollection behaviors = Interaction.GetBehaviors(Control); behaviors.Clear(); behaviors.Add(behavior); }
public override void DrawMark() { // Do :显示文本 Label t = new Label(); t.Style = this.LabelStyle; // Do :增加拖拽行为 BehaviorCollection bc = Interaction.GetBehaviors(t); bc.Add(new ElementDragStateBehavior()); this.Marker = t; Point point = this.GetPoint(); t.Content = $"({point.X.ToString("G3")},{point.Y.ToString("G3")})"; t.Loaded += (o, e) => { if (this.xAxis.Count == 1) { Canvas.SetLeft(t, this.ActualWidth / 2); Canvas.SetTop(t, this.ActualHeight / 2); } else { Canvas.SetLeft(t, this.GetX(point.X)); Canvas.SetTop(t, this.GetY(point.Y)); } }; this.Children.Add(this.Marker); }
private void ProcessText() { if (Control == null || Element == null) { return; } // Gets the complete HTML string var isRtl = Device.FlowDirection == Xamarin.Forms.FlowDirection.RightToLeft; var styledHtml = new RendererHelper(Element, Element.Text, Device.RuntimePlatform, isRtl).ToString(); if (styledHtml == null) { return; } Control.Text = styledHtml; // Adds the HtmlTextBehavior because UWP's TextBlock // does not natively support HTML content var behavior = new HtmlTextBehavior((HtmlLabel)Element); BehaviorCollection behaviors = Interaction.GetBehaviors(Control); behaviors.Clear(); behaviors.Add(behavior); }
/// <summary> /// Called when the timer has elapsed. Removes any stale notifications. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param> private static void OnTimerElapsed(WindowInfo windowInfo) { DateTime now = DateTime.Now; if (windowInfo.Window.IsMouseOver) { Observable .Timer(windowInfo.DisplayDuration) .ObserveOnDispatcher() .Subscribe(x => OnTimerElapsed(windowInfo)); } else { BehaviorCollection behaviors = Interaction.GetBehaviors(windowInfo.Window); FadeBehavior fadeBehavior = behaviors.OfType <FadeBehavior>().First(); SlideBehavior slideBehavior = behaviors.OfType <SlideBehavior>().First(); fadeBehavior.FadeOut(); slideBehavior.SlideOut(); EventHandler eventHandler = null; eventHandler = (sender2, e2) => { fadeBehavior.FadeOutCompleted -= eventHandler; windows.Remove(windowInfo); windowInfo.Window.Close(); }; fadeBehavior.FadeOutCompleted += eventHandler; } }
/// <summary> /// The get slide behaviour. /// </summary> /// <param name="frameworkElement"> The framework element. </param> /// <param name="beginTime"> The begin time. </param> /// <param name="duration"> The duration. </param> /// <param name="offset"> The offset. </param> /// <returns> The <see cref="SlideBehavior"/>. </returns> private static SlideBehavior GetSlideBehavior( FrameworkElement frameworkElement, TimeSpan beginTime, TimeSpan duration, double offset) { BehaviorCollection behaviors = Interaction.GetBehaviors(frameworkElement); SlideBehavior slideBehavior = behaviors .OfType <SlideBehavior>() .FirstOrDefault(); if (slideBehavior == null) { slideBehavior = new SlideBehavior() { IsAnimatingOnIsVisibleChanged = false, IsAnimatingOnLoaded = false }; behaviors.Add(slideBehavior); } slideBehavior.BeginTime = beginTime; slideBehavior.Duration = duration; slideBehavior.Offset = offset; return(slideBehavior); }
public AccessHandler(DependencyObject obj) { bool authorize = GetCheck(obj); BehaviorCollection behaviors = Interaction.GetBehaviors(obj); AccessBehavior authorizationBehavior = behaviors.SingleOrDefault(x => x is AccessBehavior) as AccessBehavior; if (authorizationBehavior != null && !authorize) { behaviors.Remove(authorizationBehavior); } else if (authorizationBehavior == null && authorize) { authorizationBehavior = new AccessBehavior(); var binding = BindingOperations.GetBinding(obj, TextBox.TextProperty); if (binding != null) { BindingOperations.SetBinding(authorizationBehavior, AccessBehavior.VMBoundedProperty, binding); } binding = BindingOperations.GetBinding(obj, ItemsControl.ItemsSourceProperty); if (binding != null) { BindingOperations.SetBinding(authorizationBehavior, AccessBehavior.VMBoundedProperty, binding); } binding = BindingOperations.GetBinding(obj, ButtonBase.CommandProperty); if (binding != null) { BindingOperations.SetBinding(authorizationBehavior, AccessBehavior.VMBoundedProperty, binding); } behaviors.Add(authorizationBehavior); } }
private static int GetIndexOf(BehaviorCollection itemBehaviors, Behavior behavior) { var index = -1; var orignalBehavior = GetOriginalBehavior(behavior); for (var i = 0; i < itemBehaviors.Count; i++) { var currentBehavior = itemBehaviors[i]; if (ReferenceEquals(currentBehavior, behavior) || ReferenceEquals(currentBehavior, orignalBehavior)) { index = i; break; } var currentOrignalBehavior = GetOriginalBehavior(currentBehavior); if (ReferenceEquals(currentOrignalBehavior, behavior) || ReferenceEquals(currentOrignalBehavior, orignalBehavior)) { index = i; break; } } return(index); }
public static BehaviorCollection GetBehaviors(DependencyObject d) { BehaviorCollection behaviors = (BehaviorCollection)d.GetValue(BehaviorsProperty); if(behaviors == null) { behaviors = new BehaviorCollection(); d.SetValue(BehaviorsProperty, behaviors); } return behaviors; }
/// <summary> /// Gets the <see cref="T:System.Windows.Interactivity.BehaviorCollection"/> associated with a specified object. /// /// </summary> /// <param name="obj">The object from which to retrieve the <see cref="T:System.Windows.Interactivity.BehaviorCollection"/>.</param> /// <returns> /// A <see cref="T:System.Windows.Interactivity.BehaviorCollection"/> containing the behaviors associated with the specified object. /// </returns> public static BehaviorCollection GetBehaviors(FrameworkElement obj) { BehaviorCollection behaviorCollection = (BehaviorCollection)obj.GetValue(Interaction.BehaviorsProperty); if (behaviorCollection == null) { behaviorCollection = new BehaviorCollection(); obj.SetValue(Interaction.BehaviorsProperty, behaviorCollection); } return behaviorCollection; }
static App() { try { b = System.Windows.Interactivity.Interaction.GetBehaviors(null); } catch (Exception) { } }
public static BehaviorCollection GetBehaviors(DependencyObject obj) { var behaviors = obj.GetValue(BehaviorsProperty) as BehaviorCollection; if (behaviors == null) { behaviors = new BehaviorCollection(); SetBehaviors(obj, behaviors); } return behaviors; }
public static BehaviorCollection GetBehaviors(DependencyObject obj) { if (obj == null) return null; var result = obj.GetValue(BehaviorsProperty) as BehaviorCollection; if (result == null) { result = new BehaviorCollection(); obj.SetValue(BehaviorsProperty, result); } return result; }
/// <summary> /// Behaviourを取得します。 /// </summary> public static BehaviorCollection GetBehaviors(DependencyObject target) { // BehaviorsPropertyを使って値を取得してみる var ret = (BehaviorCollection)target.GetValue(BehaviorsProperty); // 値がnullだったらインスタンスを作ってセットしておく if (ret == null) { ret = new BehaviorCollection(); target.SetValue(BehaviorsProperty, ret); } return ret; }
/// <summary> /// Gets the <see cref="BehaviorCollection" /> associated with a specified object. /// </summary> /// <param name="obj">The <see cref="EmptyKeys.UserInterface.DependencyObject" /> from which to retrieve the <see cref="BehaviorCollection" />.</param> /// <returns> /// A <see cref="BehaviorCollection" /> containing the behaviors associated with the specified object. /// </returns> /// <exception cref="System.ArgumentNullException">obj</exception> internal static BehaviorCollection GetInternalBehaviors(DependencyObject obj) { if (obj == null) { throw new ArgumentNullException("obj"); } BehaviorCollection behaviorCollection = (BehaviorCollection)obj.GetValue(Interaction.BehaviorsProperty); if (behaviorCollection == null) { behaviorCollection = new BehaviorCollection(); obj.SetValue(Interaction.BehaviorsProperty, behaviorCollection); } return behaviorCollection; }
private static void AttachBehaviors(FrameworkElement frameworkElement, BehaviorCollection behaviors) { BindingOperations.SetBinding(behaviors, FrameworkElement.DataContextProperty, new Binding("DataContext") { Source = frameworkElement, }); foreach (var behavior in behaviors) { EventInfo ei = frameworkElement.GetType().GetEvent(behavior.EventName); if (ei == null) return; Behavior preventAccessToModifiedClosureBehavior = behavior; ei.AddEventHandler(frameworkElement, new RoutedEventHandler((x, y) => { Behavior b = preventAccessToModifiedClosureBehavior; b.Command.Execute(b.CommandParameter); })); } }
/// <summary> /// Sets the collection of behaviors for the specified DependencyObject. /// </summary> /// <param name="o">The DependencyObject to set.</param> /// <param name="behaviors">The collection of behaviors to associate.</param> public static void SetBehaviors(DependencyObject o, BehaviorCollection behaviors) { o.SetValue(BehaviorsProperty, behaviors); }
/// <summary> /// Called when Property is retrieved /// </summary> /// <param name="obj"></param> /// <param name="value"></param> private static void SetBehaviors(DependencyObject obj, BehaviorCollection value) { obj.SetValue(BehaviorsProperty, value); }
public static void SetBehaviors(BindableObject target, BehaviorCollection value) { target.SetValue(BehaviorsProperty, value); }
private void GenerateBehaviors(BehaviorCollection behaviors, CodeTypeDeclaration classType, CodeMemberMethod method, FrameworkElement element, CodeExpression fieldReference) { for (int i = 0; i < behaviors.Count; i++) { var behavior = behaviors[i]; string behaviorName = element.Name + "_BEH_" + i; Type type = behavior.GetType(); CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement(type.Name, behaviorName, new CodeObjectCreateExpression(type.Name)); method.Statements.Add(variable); var behaviorVarRef = new CodeVariableReferenceExpression(behaviorName); method.Statements.Add(new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("Interaction"), "GetBehaviors(" + element.Name + ").Add", behaviorVarRef)); ValueGenerator valueGenerator = new ValueGenerator(); MethodInfo generateFieldMethod = typeof(CodeComHelper).GetMethod("GenerateField"); LocalValueEnumerator enumerator = behavior.GetLocalValueEnumerator(); while (enumerator.MoveNext()) { LocalValueEntry entry = enumerator.Current; DependencyProperty property = entry.Property; Type valueType = entry.Value.GetType(); if (CodeComHelper.IsValidForFieldGenerator(entry.Value)) { if (valueGenerator.Generators.ContainsKey(property.PropertyType) || valueGenerator.Generators.ContainsKey(valueType)) { CodeExpression propValue = valueGenerator.ProcessGenerators(classType, method, entry.Value, behaviorName); if (propValue != null) { method.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(behaviorVarRef, property.Name), propValue)); } } else if (entry.Value is ActionCollection) { GenerateBehaviorActions(entry.Value as ActionCollection, classType, method, behaviorVarRef, behaviorName); } else { MethodInfo generic = generateFieldMethod.MakeGenericMethod(property.PropertyType); if (generic == null) { throw new NullReferenceException("Generic method not created for type - " + property.PropertyType); } generic.Invoke(null, new object[] { method, behaviorVarRef, behavior, property }); } } } CodeComHelper.GenerateBindings(method, behaviorVarRef, behavior, behaviorName); CodeComHelper.GenerateResourceReferences(method, behaviorVarRef, behavior); } }