protected override Size ArrangeOverride(Size finalSize)
        {
            double      curY  = 0;
            List <Step> steps = new List <Step>();

            foreach (FrameworkElement child in Children)
            {
                Rect allocatedRect = new Rect(0, curY, finalSize.Width, child.DesiredSize.Height);
                child.Arrange(allocatedRect);
                double midPoint = finalSize.Height - ((allocatedRect.Top + allocatedRect.Bottom) / 2.0d);
                //double midPoint = finalSize.Height - ((curY + item.HeaderHeight) / 2.0d);
                if (child is ToolbarMenuItem)
                {
                    ToolbarMenuItem item = (ToolbarMenuItem)child;
                    if (!item.IsSeparator)
                    {
                        steps.Add(new Step(midPoint, item.SliderStep, item.IsStepStop));
                    }
                }
                curY += child.DesiredSize.Height;
            }

            Steps = new ObservableCollection <Step>(steps.OrderBy <Step, double>(x => x.Posision));
            return(finalSize);
        }
        public ToolbarMenuItem()
            : base()
        {
            this.AddHandler(Button.ClickEvent, (RoutedEventHandler) delegate(object sender, RoutedEventArgs e)
            {
                ////buttonContent clicked, see ToolbarTemplates.xaml
                //if (Command != null)
                //{
                //    Command.Execute(null);
                //    e.Handled = true;
                //}
            });

            this.AddHandler(ToolbarMenuItem.ClickEvent, (RoutedEventHandler) delegate(object sender, RoutedEventArgs e)
            {
                if (IsSliderEnabled)
                {
                    if (e.OriginalSource is ToolbarMenuItem)
                    {
                        ToolbarMenuItem item = (ToolbarMenuItem)e.OriginalSource;
                        SliderValue          = item.SliderStep;
                    }
                }
            });

            //Template = (ControlTemplate)this.TryFindResource("TopLevelItemTemplateKey");
            this.AddHandler(ToolbarMenuItem.LoadedEvent, (RoutedEventHandler) delegate(object sender, RoutedEventArgs e)
            {
                if (this.Role == MenuItemRole.TopLevelItem)
                {
                    Template = (ControlTemplate)this.TryFindResource("TopLevelItemTemplateKey");
                }
            });

            //Monitor Command Changed
            DependencyPropertyDescriptor isHeaderToggledescriptor = DependencyPropertyDescriptor.FromProperty
                                                                        (MenuItem.CommandProperty, typeof(ToolbarMenuItem));

            isHeaderToggledescriptor.AddValueChanged(this, new EventHandler(delegate
            {
                IsHeaderTogglePopup = Command == null;
            }));

            DependencyPropertyDescriptor roleDescriptor = DependencyPropertyDescriptor.FromProperty(MenuItem.RoleProperty, typeof(ToolbarMenuItem));

            roleDescriptor.AddValueChanged(this, new EventHandler(delegate
            {
                switch (this.Role)
                {
                case MenuItemRole.TopLevelItem:
                    Template = (ControlTemplate)this.TryFindResource("TopLevelItemTemplateKey");
                    break;

                case MenuItemRole.TopLevelHeader:
                    Template = (ControlTemplate)this.TryFindResource("TopLevelHeaderTemplateKey");
                    break;

                case MenuItemRole.SubmenuItem:
                    Template = (ControlTemplate)this.TryFindResource("SubmenuItemTemplateKey");
                    break;

                case MenuItemRole.SubmenuHeader:
                    Template = (ControlTemplate)this.TryFindResource("SubmenuHeaderTemplateKey");
                    break;
                }
            }));
        }
        private static void OnCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ToolbarMenuItem item = (ToolbarMenuItem)sender;

            item.IsHeaderTogglePopup = (e.NewValue != null);
        }