Example #1
0
        //click event


        public void UpdateMinCorrectWidth()
        {
            double result = 0;

            if (this.Template.FindName("navMenu", this) is ItemsControl itemsControl && itemsControl.IsLoaded)
            {
                for (int i = 0; i < itemsControl.Items.Count; i++)
                {
                    ContentPresenter presenter = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as ContentPresenter;

                    if (!presenter.IsLoaded)
                    {
                        continue;
                    }

                    NewNavMenuItem child = VisualTreeHelper.GetChild(presenter, 0) as NewNavMenuItem;
                    NewNavMenuItem.UpdateMinCorrectWidth(child);

                    if (child.MinCorrectWidth > result)
                    {
                        result = child.MinCorrectWidth;
                    }
                }
            }
            MinCorrectWidth = result;
        }
        public static void OnDropdownItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is NewNavMenuItem nmi)
            {
                if (nmi.DropdownItems == null || nmi.dropdownMenu == null)
                {
                    return;
                }


                for (int i = 0; i < nmi.dropdownMenu.Items.Count; i++)
                {
                    ContentPresenter presenter = nmi.dropdownMenu.ItemContainerGenerator.ContainerFromIndex(i) as ContentPresenter;
                    NewNavMenuItem   child     = VisualTreeHelper.GetChild(presenter, 0) as NewNavMenuItem;

                    if (child != null)
                    {
                        child.Clicked += (object sender, ClickedEventArgs ee) => { NewNavMenuItem_Clicked(sender, ee, nmi); }
                    }
                    ;
                }

                nmi.DropdownItems.CollectionChanged += (object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs ee) => { DropdownItems_CollectionChanged(sender, ee, nmi); };
                UpdateMinCorrectWidth(d);
            }
        }
        public static void UpdateMinCorrectWidth(DependencyObject d)
        {
            if (d is NewNavMenuItem nmi)
            {
                if (!nmi.IsLoaded)
                {
                    return;
                }

                double result = 0;
                result += nmi.IconMargin.Left;
                result += nmi.IconSize;
                result += nmi.IconMargin.Right;
                result += nmi.TextMargin.Left;
                if (nmi.textBlock != null)
                {
                    result += nmi.textBlock.ActualWidth;
                }
                result += nmi.TextMargin.Right;
                result += nmi.DropdownIconMargin.Left;
                if (nmi.dropdownIcon != null)
                {
                    result += nmi.dropdownIcon.ActualWidth;
                }
                result += nmi.DropdownIconMargin.Right;

                nmi.MinCorrectWidth = result;

                if (nmi.dropdownMenu != null)
                {
                    for (int i = 0; i < nmi.dropdownMenu.Items.Count; i++)
                    {
                        ContentPresenter presenter = nmi.dropdownMenu.ItemContainerGenerator.ContainerFromIndex(i) as ContentPresenter;

                        if (!presenter.IsLoaded)
                        {
                            continue;
                        }

                        NewNavMenuItem child = VisualTreeHelper.GetChild(presenter, 0) as NewNavMenuItem;
                        UpdateMinCorrectWidth(child);
                        if (child.IsLoaded && child.MinCorrectWidth > result)
                        {
                            result = child.MinCorrectWidth;
                        }
                    }
                }

                nmi.MinCorrectWidth = result;
            }
        }
        private static void DropdownItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, NewNavMenuItem source)
        {
            if (sender is ObservableCollection <NavMenuItemData> collection && collection == source.ItemData.DropdownItems)
            {
                if ((e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) || (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace))
                {
                    foreach (var el in e.NewItems)
                    {
                        if (el is NavMenuItemData data)
                        {
                            ContentPresenter presenter = source.dropdownMenu.ItemContainerGenerator.ContainerFromItem(data) as ContentPresenter;

                            RoutedEventHandler addClickedHandler = (object s, RoutedEventArgs ee) =>
                            {
                                NewNavMenuItem child = VisualTreeHelper.GetChild(presenter, 0) as NewNavMenuItem;

                                if (child != null)
                                {
                                    child.Clicked += (object sss, ClickedEventArgs eee) => { NewNavMenuItem_Clicked(sss, eee, source); }
                                }
                                ;
                                UpdateMinCorrectWidth(source);
                            };

                            if (presenter.IsLoaded)
                            {
                                addClickedHandler.Invoke(null, null);
                            }
                            else
                            {
                                presenter.Loaded += addClickedHandler;
                            }
                        }
                    }
                }
            }
        }
        private static void ItemData_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e, NewNavMenuItem source)
        {
            if (sender is NavMenuItemData data)
            {
                if (source.ItemData != data)
                {
                    return;
                }

                if (e.PropertyName == "ImageSource" || e.PropertyName == "")
                {
                    try { source.IconSource = new BitmapImage(source.ItemData.ImageSource); } catch { }
                }

                if (e.PropertyName == "Text" || e.PropertyName == "")
                {
                    source.Text = source.ItemData.Text;
                    UpdateMinCorrectWidth(source);
                }


                if (e.PropertyName == "DropdownItems" || e.PropertyName == "")
                {
                    source.DropdownItems = source.ItemData.DropdownItems;
                    UpdateMinCorrectWidth(source);
                }


                if (e.PropertyName == "IsDropdownItem" || e.PropertyName == "")
                {
                    source.IsDropdownable = source.ItemData.IsDropdownItem;
                    UpdateMinCorrectWidth(source);
                }


                if (e.PropertyName == "IsSelected" || e.PropertyName == "")
                {
                    source.IsSelected = source.ItemData.IsSelected;
                }
            }
        }
        private static void NewNavMenuItem_Clicked(object sender, ClickedEventArgs e, NewNavMenuItem source)
        {
            if (sender is NavMenuItemData data)
            {
                if (source.ItemData.DropdownItems?.FirstOrDefault(x => x == data) == null)
                {
                    return;
                }

                source.RaiseClickedEvent(data);
            }
        }