public CommandBarFlyoutToolBar()
        {
            SetValue(FlyoutTemplateSettingsPropertyKey, new CommandBarFlyoutCommandBarTemplateSettings());

            Loaded += delegate
            {
                UpdateUI();

                // Programmatically focus the first primary command if any, else programmatically focus the first secondary command if any.
                var commands = PrimaryCommands.Count > 0 ? PrimaryCommands : (SecondaryCommands.Count > 0 ? SecondaryCommands : null);

                if (commands != null)
                {
                    bool usingPrimaryCommands           = commands == PrimaryCommands;
                    bool ensureTabStopUniqueness        = usingPrimaryCommands || true;
                    var  firstCommandAsFrameworkElement = commands[0] as FrameworkElement;

                    if (firstCommandAsFrameworkElement != null)
                    {
                        if (SharedHelpers.IsFrameworkElementLoaded(firstCommandAsFrameworkElement))
                        {
                            FocusCommand(
                                commands,
                                usingPrimaryCommands ? m_moreButton : null /*moreButton*/,
                                true /*firstCommand*/,
                                ensureTabStopUniqueness);
                        }
                        else
                        {
                            m_firstItemLoadedRevoker = new RoutedEventHandlerRevoker(
                                firstCommandAsFrameworkElement,
                                LoadedEvent,
                                new RoutedEventHandler(delegate
                            {
                                FocusCommand(
                                    commands,
                                    usingPrimaryCommands ? m_moreButton : null /*moreButton*/,
                                    true /*firstCommand*/,
                                    ensureTabStopUniqueness);
                                m_firstItemLoadedRevoker?.Revoke();
                            }));
                        }
                    }
                }
            };

            Unloaded += delegate
            {
                StopOpenAnimation();
                SetOpacity(1);
            };

            SizeChanged += delegate
            {
                UpdateUI();
            };

            OverflowOpened += delegate
            {
                m_secondaryItemsRootSized = true;

                UpdateFlowsFromAndFlowsTo();
                UpdateUI();
            };

            OverflowClosed += delegate
            {
                m_secondaryItemsRootSized = false;

                if (PrimaryCommands.Count > 0)
                {
                    // Before RS3, ensure the focus goes to a primary command when
                    // the secondary commands are closed.
                    EnsureFocusedPrimaryCommand();
                }
            };

            AddHandler(MouseDownEvent, new MouseButtonEventHandler(OnMouseDown), true);
        }