private void SaveQatItems(RibbonQuickAccessToolBar qat)
        {
            if (qat != null)
            {
                VM.QatItemCollection qatItems = new VM.QatItemCollection();
                VM.QatItemCollection remainingItems = new VM.QatItemCollection();

                if (qat.Items.Count > 0)
                {
                    for (int qatIndex = 0; qatIndex < qat.Items.Count; qatIndex++)
                    {
                        object instance = qat.Items[qatIndex];
                        bool isSplitHeader = false;

                        if (instance is FrameworkElement)
                        {
                            FrameworkElement element = (FrameworkElement)instance;
                            instance = ((FrameworkElement)instance).DataContext;

                            isSplitHeader =
                                (instance is VM.SplitMenuItemData && element is ButtonBase) ||
                                (instance is VM.SplitButtonData && element is ButtonBase);
                        }

                        VM.QatItem qatItem = new VM.QatItem(instance, isSplitHeader);
                        qatItems.Add(qatItem);
                        remainingItems.Add(qatItem);
                    }

                    VM.RibbonData ribbonData = VM.ViewModelData.RibbonData;
                    for (int tabIndex = 0; tabIndex < ribbonData.TabDataCollection.Count && remainingItems.Count > 0; tabIndex++)
                    {
                        for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
                        {
                            VM.QatItem qatItem = remainingItems[qatIndex];
                            qatItem.TabIndex = tabIndex;
                        }

                        VM.TabData tabData = ribbonData.TabDataCollection[tabIndex];
                        for (int groupIndex = 0; groupIndex < tabData.GroupDataCollection.Count && remainingItems.Count > 0; groupIndex++)
                        {
                            VM.GroupData groupData = tabData.GroupDataCollection[groupIndex];
                            for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
                            {
                                VM.QatItem qatItem = remainingItems[qatIndex];
                                qatItem.GroupIndex = groupIndex;

                                if (qatItem.Instance == groupData)
                                {
                                    remainingItems.Remove(qatItem);
                                    qatIndex--;
                                }
                            }

                            for (int controlIndex = 0; controlIndex < groupData.ControlDataCollection.Count && remainingItems.Count > 0; controlIndex++)
                            {
                                for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
                                {
                                    VM.QatItem qatItem = remainingItems[qatIndex];
                                    qatItem.ControlIndices.Add(controlIndex);
                                }

                                VM.ControlData controlData = groupData.ControlDataCollection[controlIndex];
                                bool matched = SaveQatControlData(remainingItems, controlData);
                                if (!matched)
                                {
                                    for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
                                    {
                                        VM.QatItem qatItem = remainingItems[qatIndex];
                                        qatItem.ControlIndices.Clear();
                                    }
                                }
                            }
                        }
                    }
                }

                XmlWriter xmlWriter = XmlWriter.Create(_qatFileName);
                XamlWriter.Save(qatItems, xmlWriter);
                xmlWriter.Close();
            }
        }
        private void SaveQatItems(RibbonQuickAccessToolBar qat)
        {
            if (qat != null)
            {
                QatItemCollection qatItems = new QatItemCollection();
                QatItemCollection remainingItems = new QatItemCollection();

                if (qat.Items.Count > 0)
                {
                    for (int qatIndex = 0; qatIndex < qat.Items.Count; qatIndex++)
                    {
                        object instance = qat.Items[qatIndex];
                        bool isSplitHeader = false;

                        if (instance is FrameworkElement)
                        {
                            FrameworkElement element = (FrameworkElement)instance;

                            if (((FrameworkElement)instance).DataContext != null)
                            {
                                instance = ((FrameworkElement)instance).DataContext;
                                isSplitHeader =
                                    (instance is SplitMenuItemData && element is ButtonBase) ||
                                    (instance is SplitButtonData && element is ButtonBase);
                            }
                        }

                        QatItem qatItem = new QatItem(instance, isSplitHeader);
                        qatItems.Add(qatItem);
                        remainingItems.Add(qatItem);
                    }

                    for (int tabIndex = 0; tabIndex < ribbon.Items.Count && remainingItems.Count > 0; tabIndex++)
                    {
                        RibbonTab tab = ribbon.Items[tabIndex] as RibbonTab;
                        SaveQatItemsAmongChildren(remainingItems, tab, tabIndex);
                    }
                }

                XmlWriter xmlWriter = XmlWriter.Create(_qatFileName);
                XamlWriter.Save(qatItems, xmlWriter);
                xmlWriter.Close();
            }
        }