public void AddDropdownItem(NavMenuItemData item)
        {
            if (_dropdownItems == null)
            {
                _dropdownItems = new ObservableCollection <NavMenuItemData>();
            }

            _dropdownItems.Add(item);
        }
        public static void ResetIsSelectedFlag(this NavMenuItemData data)
        {
            data.IsSelected = false;

            if (data.DropdownItems == null)
            {
                return;
            }

            foreach (var el in data.DropdownItems)
            {
                el.ResetIsSelectedFlag();
            }
        }
Exemple #3
0
 private void RaiseClickedEvent(NavMenuItemData item)
 {
     RaiseEvent(new ClickedEventArgs(ClickedEvent, item));
 }
Exemple #4
0
 private void NavMenu_Click(object sender, ClickedEventArgs e)
 {
     LastClickedItem = e.ClickedItem;
     RaiseClickedEvent(e.ClickedItem);
 }
        private void CreateNavMenuItem(NavMenuItemData item, Panel toAdd, int offset = 0)
        {
            #region создание самого элемента


            DockPanel dock = new DockPanel()
            {
                Height     = ItemHeight,
                Background = new SolidColorBrush(item.IsSelected ? SelectedItemBackground : Background)
            };

            Image icon = null;
            try
            {
                icon = new Image()
                {
                    Height = IconSize,
                    Width  = IconSize,
                    Margin = new Thickness((DropdownIconSectionWidth - IconSize) / 2 + offset, (ItemHeight - IconSize) / 2, (DropdownIconSectionWidth - IconSize) / 2, (ItemHeight - IconSize) / 2),
                    Source = new BitmapImage(item.ImageSource)
                };
                DockPanel.SetDock(icon, Dock.Left);
                dock.Children.Add(icon);
            }
            catch { }


            TextBlock text = new TextBlock()
            {
                VerticalAlignment   = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Left,
                Text       = item.Text,
                Foreground = item.IsSelected ? new SolidColorBrush(SelectedItemTextColor) : new SolidColorBrush(ItemTextColor),
                FontFamily = ItemTextFontFamily,
                FontSize   = ItemTextFontSize,
                FontWeight = ItemTextFontWeight,
                Margin     = new Thickness(0, 0, (ItemHeight - ItemTextFontSize) / 2, 0)
            };
            DockPanel.SetDock(text, Dock.Left);
            dock.Children.Add(text);



            ColorAnimation mouseEnterAnimation = new ColorAnimation()
            {
                From     = item.IsSelected ? SelectedItemBackground : Background,
                To       = MouseInItemBackground,
                Duration = MouseInOverAnimationDuration
            };
            ColorAnimation mouseEnterTextAnimation = new ColorAnimation()
            {
                From     = item.IsSelected ? SelectedItemTextColor : ItemTextColor,
                To       = MouseInItemTextColor,
                Duration = MouseInOverAnimationDuration
            };
            dock.MouseEnter += (object sender, MouseEventArgs e) =>
            {
                if (!this.IsEnabled)
                {
                    return;
                }

                dock.Background.BeginAnimation(SolidColorBrush.ColorProperty, mouseEnterAnimation);
                text.Foreground.BeginAnimation(SolidColorBrush.ColorProperty, mouseEnterTextAnimation);
            };

            ColorAnimation mouseLeaveAnimation = new ColorAnimation()
            {
                From     = MouseInItemBackground,
                To       = item.IsSelected ? SelectedItemBackground : Background,
                Duration = MouseInOverAnimationDuration
            };
            ColorAnimation mouseLeaveTextAnimation = new ColorAnimation()
            {
                To       = item.IsSelected ? SelectedItemTextColor : ItemTextColor,
                From     = MouseInItemTextColor,
                Duration = MouseInOverAnimationDuration
            };
            dock.MouseLeave += (object sender, MouseEventArgs e) =>
            {
                if (!this.IsEnabled)
                {
                    return;
                }

                dock.Background.BeginAnimation(SolidColorBrush.ColorProperty, mouseLeaveAnimation);
                text.Foreground.BeginAnimation(SolidColorBrush.ColorProperty, mouseLeaveTextAnimation);
            };

            MouseClickManager clickManager = new MouseClickManager(200, () => { return(this.IsEnabled); });
            dock.MouseLeftButtonDown += clickManager.OnMouseLeftButtonDown;
            dock.MouseLeftButtonUp   += clickManager.OnMouseLeftButtonUp;

            //обработчик кликов во вне
            clickManager.Click += (object sender, MouseButtonEventArgs e) => { RaiseClickedEvent(item); };

            item.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) =>
            {
                _context?.Post((s) =>
                {
                    if (sender is NavMenuItemData data)
                    {
                        if (e.PropertyName == "ImageSource")
                        {
                            if (icon != null)
                            {
                                icon.Source = new BitmapImage(data.ImageSource);
                            }
                        }

                        if (e.PropertyName == "Text")
                        {
                            if (text != null)
                            {
                                text.Text = data.Text;
                            }
                        }

                        if (e.PropertyName == "IsSelected")
                        {
                            if (text != null)
                            {
                                text.Foreground = data.IsSelected ? new SolidColorBrush(SelectedItemTextColor) : new SolidColorBrush(ItemTextColor);
                            }

                            if (dock != null)
                            {
                                dock.Background = new SolidColorBrush(data.IsSelected ? SelectedItemBackground : Background);
                            }

                            if (mouseEnterAnimation != null)
                            {
                                mouseEnterAnimation.From = data.IsSelected ? SelectedItemBackground : Background;
                            }

                            if (mouseEnterTextAnimation != null)
                            {
                                mouseEnterTextAnimation.From = data.IsSelected ? SelectedItemTextColor : ItemTextColor;
                            }

                            if (mouseLeaveAnimation != null)
                            {
                                mouseLeaveAnimation.To = data.IsSelected ? SelectedItemBackground : Background;
                            }

                            if (mouseLeaveTextAnimation != null)
                            {
                                mouseLeaveTextAnimation.To = data.IsSelected ? SelectedItemTextColor : ItemTextColor;
                            }
                        }
                    }
                }, null);
            };


            toAdd.Children.Add(dock);

            #endregion

            #region создание подменю

            bool isAnimatedNow = false;
            if (item.IsDropdownItem && item.DropdownItems != null)
            {
                Image dropdownIcon;
                try
                {
                    dropdownIcon = new Image()
                    {
                        Height = DropdownIconSize,
                        Width  = DropdownIconSize,
                        Margin =
                            new Thickness((ItemHeight - DropdownIconSize) / 2.0 > DropdownIconMinLeftOffset ? (ItemHeight - DropdownIconSize) / 2.0 : DropdownIconMinLeftOffset,
                                          (ItemHeight - DropdownIconSize) / 2,
                                          (ItemHeight - DropdownIconSize) / 2,
                                          (ItemHeight - DropdownIconSize) / 2),

                        HorizontalAlignment = HorizontalAlignment.Right,
                        Source          = new BitmapImage(DropdownIconSource),
                        RenderTransform = new RotateTransform(0, DropdownIconSize / 2, DropdownIconSize / 2),
                        Name            = "dropdownIcon"
                    };
                    DockPanel.SetDock(dropdownIcon, Dock.Right);
                    dock.Children.Add(dropdownIcon);
                }
                catch { }


                DoubleAnimation rotateAnimation = new DoubleAnimation()
                {
                    Duration       = DropdownMenuAnimationDuration,
                    EasingFunction = DropdownMenuFunction,
                };

                StackPanel dropdownMenu = new StackPanel()
                {
                    Orientation = Orientation.Vertical,
                    MaxHeight   = 0
                };



                foreach (var el in item.DropdownItems)
                {
                    CreateNavMenuItem(el, dropdownMenu, offset + DropdownMenuLeftOffset);
                }


                DoubleAnimation dropupAnimation = new DoubleAnimation()
                {
                    Duration       = DropdownMenuAnimationDuration,
                    EasingFunction = DropdownMenuFunction
                };

                DoubleAnimationUsingKeyFrames dropdownAnimation = new DoubleAnimationUsingKeyFrames();
                dropdownAnimation.KeyFrames.Add(new EasingDoubleKeyFrame()
                {
                    KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = 0, EasingFunction = DropdownMenuFunction
                });
                dropdownAnimation.KeyFrames.Add(new EasingDoubleKeyFrame()
                {
                    KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(DropdownMenuAnimationDuration.Milliseconds - 1)), EasingFunction = DropdownMenuFunction
                });
                dropdownAnimation.KeyFrames.Add(new EasingDoubleKeyFrame()
                {
                    KeyTime = KeyTime.FromTimeSpan(DropdownMenuAnimationDuration), Value = 100000
                });

                this.IsEnabledChanged += (object sender, DependencyPropertyChangedEventArgs e) =>
                {
                    if (!(bool)e.NewValue && dropdownMenu.MaxHeight > 0)
                    {
                        dropupAnimation.From = dropdownMenu.RenderSize.Height;
                        dropupAnimation.To   = 0;
                        dropdownMenu.BeginAnimation(Panel.MaxHeightProperty, dropupAnimation);
                    }
                };


                #region обработчик кликов для скрытия/раскрытия меню

                rotateAnimation.Completed += (object sender, EventArgs e) => { isAnimatedNow = false; };

                clickManager.Click += (object sender, MouseButtonEventArgs e) =>
                {
                    if (!isAnimatedNow && (sender is Panel senderPanel))
                    {
                        if (senderPanel.Parent is Panel parentPanel)
                        {
                            var submenu = parentPanel.Children[parentPanel.Children.IndexOf(senderPanel) + 1];

                            if (submenu is Panel submenuPanel)
                            {
                                isAnimatedNow = true;
                                if (submenuPanel.MaxHeight <= 0.1)
                                {
                                    rotateAnimation.From = 0;
                                    rotateAnimation.To   = 180;
                                    dropdownAnimation.KeyFrames[1].Value = submenuPanel.RenderSize.Height;
                                    submenuPanel.BeginAnimation(Panel.MaxHeightProperty, dropdownAnimation);
                                }
                                else
                                {
                                    rotateAnimation.From = 180;
                                    rotateAnimation.To   = 360;
                                    dropupAnimation.From = submenuPanel.RenderSize.Height;
                                    dropupAnimation.To   = 0;
                                    submenuPanel.BeginAnimation(Panel.MaxHeightProperty, dropupAnimation);
                                }


                                if ((LogicalTreeHelper.FindLogicalNode(senderPanel, "dropdownIcon") is Image img) && (img.RenderTransform is RotateTransform transform))
                                {
                                    transform.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);
                                }
                            }
                        }
                    }
                };
                #endregion

                toAdd.Children.Add(dropdownMenu);
            }

            #endregion
        }