private void LoadQatItems(RibbonQuickAccessToolBar qat)
        {
            if (qat != null)
            {
                if (File.Exists(_qatFileName))
                {
                    XmlReader xmlReader = XmlReader.Create(_qatFileName);
                    VM.QatItemCollection qatItems = (VM.QatItemCollection)XamlReader.Load(xmlReader);
                    xmlReader.Close();
                    if (qatItems != null)
                    {
                        int remainingItemsCount = qatItems.Count;
                        VM.QatItemCollection tabMatchedItems = new VM.QatItemCollection();
                        VM.QatItemCollection groupMatchedItems = new VM.QatItemCollection();

                        if (qatItems.Count > 0)
                        {
                            VM.RibbonData ribbonData = VM.ViewModelData.RibbonData;
                            for (int tabIndex = 0; tabIndex < ribbonData.TabDataCollection.Count && remainingItemsCount > 0; tabIndex++)
                            {
                                tabMatchedItems.Clear();
                                for (int qatIndex = 0; qatIndex < qatItems.Count; qatIndex++)
                                {
                                    VM.QatItem qatItem = qatItems[qatIndex];
                                    if (qatItem.TabIndex == tabIndex)
                                    {
                                        tabMatchedItems.Add(qatItem);
                                    }
                                }

                                VM.TabData tabData = ribbonData.TabDataCollection[tabIndex];
                                for (int groupIndex = 0; groupIndex < tabData.GroupDataCollection.Count && tabMatchedItems.Count > 0 && remainingItemsCount > 0; groupIndex++)
                                {
                                    VM.GroupData groupData = tabData.GroupDataCollection[groupIndex];

                                    groupMatchedItems.Clear();
                                    for (int qatIndex = 0; qatIndex < tabMatchedItems.Count; qatIndex++)
                                    {
                                        VM.QatItem qatItem = tabMatchedItems[qatIndex];
                                        if (qatItem.GroupIndex == groupIndex)
                                        {
                                            if (qatItem.ControlIndices.Count == 0)
                                            {
                                                qatItem.Instance = groupData;
                                                remainingItemsCount--;
                                            }
                                            else
                                            {
                                                groupMatchedItems.Add(qatItem);
                                            }
                                        }
                                    }

                                    for (int controlIndex = 0; controlIndex < groupData.ControlDataCollection.Count && groupMatchedItems.Count > 0 && remainingItemsCount > 0; controlIndex++)
                                    {
                                        VM.ControlData controlData = groupData.ControlDataCollection[controlIndex];
                                        LoadQatControlData(groupMatchedItems, 0, controlIndex, controlData, ref remainingItemsCount);
                                    }
                                }
                            }

                            for (int qatIndex = 0; qatIndex < qatItems.Count; qatIndex++)
                            {
                                VM.QatItem qatItem = qatItems[qatIndex];
                                Control control = MapQatDataToControl(qatItem);
                                if (control != null)
                                {
                                    qat.Items.Add(control);
                                }
                            }
                        }
                    }
                }
            }
        }
        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 LoadQatControlData(
            VM.QatItemCollection previouslyMatchedItems,
            int matchLevel,
            int controlIndex,
            VM.ControlData controlData,
            ref int remainingItemsCount)
        {
            VM.QatItemCollection matchedItems = new VM.QatItemCollection();
            for (int qatIndex = 0; qatIndex < previouslyMatchedItems.Count; qatIndex++)
            {
                VM.QatItem qatItem = previouslyMatchedItems[qatIndex];
                if (qatItem.ControlIndices[matchLevel] == controlIndex)
                {
                    if (qatItem.ControlIndices.Count == matchLevel + 1)
                    {
                        qatItem.Instance = controlData;
                        remainingItemsCount--;
                    }
                    else if (qatItem.ControlIndices.Count == matchLevel + 2 && qatItem.ControlIndices[matchLevel + 1] == -1)
                    {
                        qatItem.IsSplitHeader = true;
                        qatItem.Instance = controlData;
                        remainingItemsCount--;
                    }
                    else
                    {
                        matchedItems.Add(qatItem);
                    }
                }
            }

            VM.MenuButtonData menuButtonData = controlData as VM.MenuButtonData;
            if (menuButtonData != null)
            {
                for (controlIndex = 0; controlIndex < menuButtonData.ControlDataCollection.Count && matchedItems.Count > 0 && remainingItemsCount > 0; controlIndex++)
                {
                    controlData = menuButtonData.ControlDataCollection[controlIndex];
                    LoadQatControlData(matchedItems, matchLevel + 1, controlIndex, controlData, ref remainingItemsCount);
                }
            }
        }