Ejemplo n.º 1
0
        public void VectorChanged_ReplaceWhileAttached_OldDetachedNewAttached()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection();

            behaviorCollection.Attach(new Button());

            StubBehavior first = new StubBehavior();

            behaviorCollection.Add(first);

            StubBehavior second = new StubBehavior();

            behaviorCollection[0] = second;

            TestUtilities.AssertDetached(first);

            TestUtilities.AssertAttached(second, behaviorCollection.AssociatedObject);
        }
Ejemplo n.º 2
0
        public void VectorChanged_ResetWhileNotAttached_DetachNotCalled()
        {
            StubBehavior[] behaviorArray = { new StubBehavior(), new StubBehavior(), new StubBehavior() };

            BehaviorCollection behaviorCollection = new BehaviorCollection();

            foreach (StubBehavior behavior in behaviorArray)
            {
                behaviorCollection.Add(behavior);
            }

            behaviorCollection.Clear();

            foreach (StubBehavior behavior in behaviorArray)
            {
                TestUtilities.AssertNotDetached(behavior);
            }
        }
Ejemplo n.º 3
0
        public static void ApplyBehavior <T>(this DependencyObject d) where T : Behavior, new()
        {
            if (d == null)
            {
                return;
            }

            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(d);

            foreach (var behavior in itemBehaviors)
            {
                if ((behavior as T) == null)
                {
                    continue;
                }
                itemBehaviors.Remove(behavior);
            }
            itemBehaviors.Add(new T());
        }
Ejemplo n.º 4
0
        private static void OnPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            UIElement uIElement = dpo as UIElement;

            if (uIElement == null)
            {
                return;
            }
            BehaviorCollection         behaviors = Interaction.GetBehaviors(uIElement);
            StylizedBehaviorCollection newValue  = e.NewValue as StylizedBehaviorCollection;
            StylizedBehaviorCollection oldValue  = e.OldValue as StylizedBehaviorCollection;

            if (newValue == oldValue)
            {
                return;
            }
            if (oldValue != null)
            {
                foreach (Behavior behavior in oldValue)
                {
                    int indexOf = StylizedBehaviors.GetIndexOf(behaviors, behavior);
                    if (indexOf < 0)
                    {
                        continue;
                    }
                    behaviors.RemoveAt(indexOf);
                }
            }
            if (newValue != null)
            {
                foreach (Behavior behavior1 in newValue)
                {
                    if (StylizedBehaviors.GetIndexOf(behaviors, behavior1) >= 0)
                    {
                        continue;
                    }
                    Behavior behavior2 = (Behavior)behavior1.Clone();
                    StylizedBehaviors.SetOriginalBehavior(behavior2, behavior1);
                    behaviors.Add(behavior2);
                }
            }
        }
Ejemplo n.º 5
0
        private void addColumnWidthBehavior(Grid g, int columnIndex, TabControl tabControl)
        {
            BehaviorCollection behaviors = Interaction.GetBehaviors(g);

            foreach (Behavior b in behaviors)
            {
                if (b is AdjustColumnMinWidthToFitTabsBehavior)
                {
                    return;
                }
            }

            AdjustColumnMinWidthToFitTabsBehavior widthBehavior = new AdjustColumnMinWidthToFitTabsBehavior()
            {
                Column     = columnIndex,
                TabControl = tabControl
            };

            behaviors.Add(widthBehavior);
        }
        private static void OnTemplateChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            InteractivityTemplate dt = (InteractivityTemplate)e.NewValue;

        #if (!SILVERLIGHT)
            dt.Seal();
        #endif
            InteractivityItems ih = (InteractivityItems)dt.LoadContent();
            BehaviorCollection bc = Interaction.GetBehaviors(d);
            System.Windows.Interactivity.TriggerCollection tc = Interaction.GetTriggers(d);
            foreach (Behavior behavior in ih.Behaviors)
            {
                bc.Add(behavior);
            }
            foreach (System.Windows.Interactivity.TriggerBase trigger in ih.Triggers)
            {
                tc.Add(trigger);
            }
        }
Ejemplo n.º 7
0
        public void AttachBehaviorMultipleElements_ShouldThrow()
        {
            StubBehavior behavior = new StubBehavior();

            Button             button1    = new Button();
            BehaviorCollection behaviors1 = Interaction.GetBehaviors(button1);

            Button             button2    = new Button();
            BehaviorCollection behaviors2 = Interaction.GetBehaviors(button2);

            behaviors1.Add(behavior);

            try
            {
                behaviors2.Add(behavior);
                Assert.Fail("InvalidOperationexception should be thrown if same behavior is attached to two elements");
            }
            catch (InvalidOperationException)
            {
            }
        }
Ejemplo n.º 8
0
        public static void ApplyBehavior <T>(this DependencyObject p_dependencyObject) where T : Behavior, new()
        {
            if (p_dependencyObject == null)
            {
                return;
            }

            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(p_dependencyObject);

            for (int i = 0; i < itemBehaviors.Count; i++)
            {
                var behavior = itemBehaviors[i];

                if (!(behavior is T))
                {
                    continue;
                }
                itemBehaviors.Remove(behavior);
            }

            itemBehaviors.Add(new T());
        }
        private void addRemoveMapBehavior(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;
            }

            if (extensionBehavior.IsEnabled)
            {
                if (!behaviors.Contains(mapBehavior))
                {
                    behaviors.Add(mapBehavior);
                }
            }
            else
            {
                behaviors.Remove(mapBehavior);
            }
        }
Ejemplo n.º 10
0
        private static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
            {
                return;
            }
            InteractivityTemplate dt = (InteractivityTemplate)e.NewValue;

            dt.Seal();
            InteractivityItems ih = (InteractivityItems)dt.LoadContent();
            BehaviorCollection bc = Interaction.GetBehaviors(d);
            TriggerCollection  tc = Interaction.GetTriggers(d);

            foreach (Behavior behavior in ih.Behaviors)
            {
                bc.Add(behavior);
            }

            foreach (TriggerBase trigger in ih.Triggers)
            {
                tc.Add(trigger);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// The initial collection animation.
        /// </summary>
        /// <param name="itemsControl"> The items control. </param>
        private static void InitCollectionAnimation(ItemsControl itemsControl)
        {
            Wizard     wizard     = itemsControl.FindVisualParent <Wizard>();
            WizardItem wizardItem = itemsControl.FindVisualParent <WizardItem>();

            if ((wizard != null) && (wizardItem != null))
            {
                WizardCollectionAnimation animation = (WizardCollectionAnimation)Enum.Parse(
                    typeof(WizardCollectionAnimation),
                    GetCollectionAnimation(itemsControl).ToString());

                BehaviorCollection            behaviors = Interaction.GetBehaviors(itemsControl);
                AnimatingItemsControlBehavior behavior  = behaviors.OfType <AnimatingItemsControlBehavior>().FirstOrDefault();

                if (behavior == null)
                {
                    behavior = new AnimatingItemsControlBehavior()
                    {
                        Duration = wizard.TransitionDuration.TimeSpan,
                        IsRandom = animation == WizardCollectionAnimation.Random
                    };
                    behaviors.Add(behavior);
                }

                wizardItem.Entering +=
                    (sender, e2) =>
                {
                    behavior.AnimateIn();
                };
                wizardItem.Leaving +=
                    (sender, e2) =>
                {
                    behavior.AnimateOut();
                };
            }
        }
Ejemplo n.º 12
0
        void AssociatedObject_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (AssociatedObject == null || _skipHandler || ((bool)e.NewValue && PopupWindow != null && PopupWindow.IsVisible))
            {
                return;
            }

            var app = Application.Current as App;

            Action execute = () => {
                Dispatcher.BeginInvoke((Action)(() =>
                {
                    lock (this)
                    {
                        if (PopupWindow == null)
                        {
                            TextInfo ti = CultureInfo.CurrentUICulture.TextInfo;
                            var owenr = AssociatedObject.TryFindParent <Window>();

                            PopupWindow = new Window();
                            PopupWindow.SizeToContent = SizeToContent.WidthAndHeight;
                            PopupWindow.AllowsTransparency = true;
                            PopupWindow.ShowInTaskbar = false;
                            PopupWindow.Title = ti.ToTitleCase(AssociatedObject.Name);
                            PopupWindow.Name = PopupWindow.Title;
                            PopupWindow.Background = Brushes.Transparent;
                            PopupWindow.WindowStyle = WindowStyle.None;
                            PopupWindow.DataContext = AssociatedObject.DataContext;
                            PopupWindow.Owner = owenr;
                            PopupWindow.Activated += (s, args) =>
                            {
                                if (Mouse.Captured != null)
                                {
                                    Mouse.Captured.ReleaseMouseCapture();
                                }
                            };
                            PopupWindow.SizeChanged += (s, args) =>
                            {
                                if (Mouse.Captured != null)
                                {
                                    Mouse.Captured.ReleaseMouseCapture();
                                }
                            };
                            PopupWindow.IsVisibleChanged += (s, args) =>
                            {
                                if (Mouse.Captured != null)
                                {
                                    Mouse.Captured.ReleaseMouseCapture();
                                }
                            };
                            //PopupWindow.Topmost = true;
                            PopupWindow.MinHeight = 200;
                            if (double.IsNaN(AssociatedObject.MinHeight) != true)
                            {
                                PopupWindow.MinHeight = AssociatedObject.MinHeight;
                            }
                            PopupWindow.MinWidth = 200;
                            if (double.IsNaN(AssociatedObject.MinWidth) != true)
                            {
                                PopupWindow.MinWidth = AssociatedObject.MinWidth;
                            }
                            PopupWindow.Icon = owenr.Icon;
                            PopupWindow.ResizeMode = ResizeMode.CanResizeWithGrip;

                            if (double.IsNaN(AssociatedObject.Height) != true)
                            {
                                PopupWindow.Height = AssociatedObject.Height;
                                PopupWindow.ResizeMode = ResizeMode.NoResize;
                            }
                            if (double.IsNaN(AssociatedObject.Width) != true)
                            {
                                PopupWindow.Width = AssociatedObject.Width;
                                PopupWindow.ResizeMode = ResizeMode.NoResize;
                            }

                            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(PopupWindow);
                            var bh = new WindowStateBehavior();
                            itemBehaviors.Add(bh);

                            if (bh.WindowStateSettings.Left != -1)
                            {
                                PopupWindow.WindowStartupLocation = WindowStartupLocation.Manual;
                            }
                            else
                            {
                                PopupWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                            }

                            _isInitialized = true;
                        }

                        if ((bool)(sender as FrameworkElement).GetValue(IsAutomaticProperty) == false)
                        {
                            return;
                        }

                        if ((bool)e.NewValue)
                        {
                            Debug.WriteLine("ShowingInPopup");
                            Show();
                        }
                        else
                        {
                            Debug.WriteLine("Closing Popup");
                            Hide();
                        }
                    }
                }), System.Windows.Threading.DispatcherPriority.ApplicationIdle);
            };

            if (app.Initialized)
            {
                execute();
            }
            else
            {
                app.InitializationSequence[this] = execute;
            }
        }
Ejemplo n.º 13
0
        /*
         * 显示大图
         */
        public static void showBigImage(string imagePath, DControlDto ctl, Grid mainContainer, Cfg cfg)
        {
            FrameworkElement bigImageElement = FrameworkElementUtil.getByName(mainContainer, "bigImageCanvas");

            if (bigImageElement == null)
            {
                Canvas bigImageCanvas = new Canvas();
                bigImageCanvas.Name       = "bigImageCanvas";
                bigImageCanvas.Width      = cfg.screenWidth;
                bigImageCanvas.Height     = cfg.screenHeight;
                bigImageCanvas.Background = Brushes.Transparent;
                Panel.SetZIndex(bigImageCanvas, 10002);

                //图片背景,点击隐藏
                Border borderBg = new Border();
                borderBg.Width               = cfg.screenWidth;
                borderBg.Height              = cfg.screenHeight;
                borderBg.Background          = Brushes.Black;
                borderBg.Opacity             = 0.6;
                borderBg.HorizontalAlignment = HorizontalAlignment.Left;
                borderBg.VerticalAlignment   = VerticalAlignment.Top;
                borderBg.SetValue(Canvas.LeftProperty, 0.0);
                borderBg.SetValue(Canvas.TopProperty, 0.0);
                Panel.SetZIndex(borderBg, 1);
                bigImageCanvas.Children.Add(borderBg);



                //显示大图
                double      maxWidth    = cfg.screenWidth * 0.9;
                double      maxHeight   = cfg.screenHeight * 0.9;
                string      img         = FileUtil.notExistsShowDefault(imagePath, Params.ImageNotExists);
                BitmapImage bitmapImage = FileUtil.readImage(AppDomain.CurrentDomain.BaseDirectory + img);
                double      showWidth   = bitmapImage.Width;
                double      showHeight  = bitmapImage.Height;
                double      w           = bitmapImage.Width / maxWidth;
                double      h           = bitmapImage.Height / maxHeight;
                if (w > 1 && h > 1)
                {
                    if (w > h)
                    {
                        showWidth  = maxWidth;
                        showHeight = bitmapImage.Height / w;
                    }
                    else
                    {
                        showHeight = maxHeight;
                        showWidth  = bitmapImage.Width / h;
                    }
                }
                else if (w > 1)
                {
                    showWidth  = maxWidth;
                    showHeight = bitmapImage.Height / w;
                }
                else if (h > 1)
                {
                    showHeight = maxHeight;
                    showWidth  = bitmapImage.Width / h;
                }

                Canvas innerCanvas = new Canvas();
                innerCanvas.Width  = showWidth;
                innerCanvas.Height = showHeight;
                double left = ctl.left;
                double top  = ctl.top;
                innerCanvas.SetValue(Canvas.LeftProperty, left);
                innerCanvas.SetValue(Canvas.TopProperty, top);
                Panel.SetZIndex(innerCanvas, 2);


                Image image = new Image();
                image.Source = bitmapImage;
                image.Width  = showWidth;
                image.Height = showHeight;
                Panel.SetZIndex(image, 1);
                innerCanvas.Children.Add(image);

                Button closebtn = new Button();
                closebtn.Background = new ImageBrush
                {
                    ImageSource = FileUtil.readImage(AppDomain.CurrentDomain.BaseDirectory + "/myfile/sysimg/ico-image-close.png")
                    ,
                    Stretch = Stretch.Fill
                };

                closebtn.Width               = 50;
                closebtn.Height              = 50;
                closebtn.BorderThickness     = new Thickness(0);
                closebtn.HorizontalAlignment = HorizontalAlignment.Right;
                closebtn.VerticalAlignment   = VerticalAlignment.Top;
                closebtn.SetValue(Canvas.RightProperty, -25.0);
                closebtn.SetValue(Canvas.TopProperty, -25.0);
                Panel.SetZIndex(closebtn, 2);

                closebtn.Click            += (sender, e) => imageCloseBtnClick(bigImageCanvas, borderBg, innerCanvas, ctl, mainContainer);
                closebtn.PreviewTouchDown += imageCloseBtnTouchDown;
                closebtn.PreviewTouchMove += imageCloseBtnTouchMove;
                closebtn.PreviewTouchUp   += (sender, e) => imageCloseBtnClick(bigImageCanvas, borderBg, innerCanvas, ctl, mainContainer);
                innerCanvas.Children.Add(closebtn);


                bigImageCanvas.Children.Add(innerCanvas);
                borderBg.PreviewMouseUp += (sender, e) => imageCloseBtnClick(bigImageCanvas, borderBg, innerCanvas, ctl, mainContainer);
                borderBg.PreviewTouchUp += (sender, e) => imageCloseBtnClick(bigImageCanvas, borderBg, innerCanvas, ctl, mainContainer);

                //手指缩放 移动 旋转
                BehaviorCollection          behaviors = Interaction.GetBehaviors(innerCanvas);
                TranslateZoomRotateBehavior tz        = new TranslateZoomRotateBehavior();
                tz.TranslateFriction       = 0.3;
                tz.RotationalFriction      = 0.4;
                tz.ConstrainToParentBounds = true;
                tz.SupportedGestures       = ManipulationModes.All;
                behaviors.Add(tz);

                TransformGroup group          = new TransformGroup();
                double         scaleX         = ctl.width / innerCanvas.Width;
                double         scaleY         = ctl.height / innerCanvas.Height;
                ScaleTransform scaleTransform = new ScaleTransform();
                scaleTransform.ScaleX = scaleX;
                scaleTransform.ScaleY = scaleY;
                group.Children.Add(scaleTransform);
                innerCanvas.RenderTransform = group;

                //动画,平移到指定位置
                double toLeft = (cfg.screenWidth - innerCanvas.Width) / 2;
                double toTop  = (cfg.screenHeight - innerCanvas.Height) / 2;
                double toTranslateTransformX = toLeft - ctl.left;
                double toTranslateTransformY = toTop - ctl.top;

                ShowBigImageUtil.showAnimation(borderBg, innerCanvas, ctl, toTranslateTransformX, toTranslateTransformY);

                mainContainer.Children.Add(bigImageCanvas);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// The initial animation.
        /// </summary>
        /// <param name="frameworkElement"> The framework element. </param>
        private static void InitAnimation(FrameworkElement frameworkElement)
        {
            Wizard     wizard     = frameworkElement.FindVisualParent <Wizard>();
            WizardItem wizardItem = frameworkElement.FindVisualParent <WizardItem>();

            if ((wizard != null) && (wizardItem != null))
            {
                WizardAnimation animation = (WizardAnimation)Enum.Parse(
                    typeof(WizardAnimation),
                    GetAnimation(frameworkElement).ToString());

                BehaviorCollection behaviors = Interaction.GetBehaviors(frameworkElement);

                FadeBehavior fadeBehavior = null;
                if ((animation == WizardAnimation.Fade) || (animation == WizardAnimation.FadeAndSlide))
                {
                    fadeBehavior = behaviors.OfType <FadeBehavior>().FirstOrDefault();
                    if (fadeBehavior == null)
                    {
                        fadeBehavior = new FadeBehavior()
                        {
                            Duration = wizard.TransitionDuration.TimeSpan,
                        };
                        behaviors.Add(fadeBehavior);
                    }
                }

                SlideBehavior slideBehavior = null;
                if ((animation == WizardAnimation.Slide) || (animation == WizardAnimation.FadeAndSlide))
                {
                    slideBehavior = behaviors.OfType <SlideBehavior>().FirstOrDefault();
                    if (slideBehavior == null)
                    {
                        slideBehavior = new SlideBehavior()
                        {
                            Duration = wizard.TransitionDuration.TimeSpan,
                        };
                        behaviors.Add(slideBehavior);
                    }
                }

                wizardItem.Entering +=
                    (sender, e2) =>
                {
                    if (fadeBehavior != null)
                    {
                        fadeBehavior.FadeIn();
                    }

                    if (slideBehavior != null)
                    {
                        slideBehavior.SlideIn();
                    }
                };
                wizardItem.Leaving +=
                    (sender, e2) =>
                {
                    if (fadeBehavior != null)
                    {
                        fadeBehavior.FadeOut();
                    }

                    if (slideBehavior != null)
                    {
                        slideBehavior.SlideOut();
                    }
                };
            }
        }
Ejemplo n.º 15
0
        private static void OnPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            bool designTime = System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject());

            if (designTime)
            {
                return;
            }

            var uie = dpo as UIElement;

            if (uie == null)
            {
                return;
            }

            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(uie);

            var newBehaviors = e.NewValue as StylizedBehaviorCollection;
            var oldBehaviors = e.OldValue as StylizedBehaviorCollection;

            if (newBehaviors == oldBehaviors)
            {
                return;
            }

            if (oldBehaviors != null)
            {
                foreach (var behavior in oldBehaviors)
                {
                    int index = itemBehaviors.IndexOf(behavior);

                    if (index >= 0)
                    {
                        itemBehaviors.RemoveAt(index);
                    }
                }
            }

            if (itemBehaviors != null)
            {
                while (itemBehaviors.Count > 0)
                {
                    itemBehaviors[0].Detach();
                    itemBehaviors.RemoveAt(0);
                }
            }

            if (newBehaviors != null)
            {
                foreach (var behavior in newBehaviors)
                {
                    int index = itemBehaviors.IndexOf(behavior);

                    if (index < 0)
                    {
                        var dps = GetDependencyProperties(behavior, false);
                        var beh = (Behavior)Activator.CreateInstance(behavior.GetType());
                        foreach (var dp in dps)
                        {
                            beh.SetValue(dp, behavior.GetValue(dp));
                        }
                        beh.Attach(dpo);
                        itemBehaviors.Add(beh);
                    }
                }
            }
        }
Ejemplo n.º 16
0
        private static void AttachBehavior(Behavior behavior, DependencyObject dependencyObject)
        {
            BehaviorCollection behaviors = Interaction.GetBehaviors(dependencyObject);

            behaviors.Add(behavior);
        }