Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
90
Persist Quick Access Toolbar Selections
posted

Is there a simple way to persist the Quick Access Toolbar selections made by a user?

  • 5
    posted

    I think this one might help:

    xaml:

    <igRibbon:XamRibbonWindow 
        x:Class="frmTest_xamRibbonSaveButtonsFromQuickAccessBar"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:igRibbon="http://infragistics.com/Ribbon"
        Title="Test" 
        Height="305" 
        Width="587" 
        ResizeMode="NoResize" 
        FontWeight="Normal" 
        MinHeight="305" 
        Loaded="XamRibbonWindow_Loaded" 
        Closing="XamRibbonWindow_Closing">

        <igRibbon:RibbonWindowContentHost>
            <igRibbon:RibbonWindowContentHost.Ribbon>
                <igRibbon:XamRibbon x:Name="xamRibbon" >
                    <igRibbon:RibbonTabItem Header="Tab1">
                        <igRibbon:RibbonGroup Id="grpTestGroup1" Name="grpTestGroup1" Caption="Test Group 1">
                            <igRibbon:ButtonTool Id="btnButton1" Name="btnButton1" Content="Button 1" />
                            <igRibbon:ButtonTool Id="btnButton2" Name="btnButton2" Content="Button 2" />
                            <igRibbon:ButtonTool Id="btnButton3" Name="btnButton3" Content="Button 3" />
                            <igRibbon:ToggleButtonTool 
                                Id="chkToggleButton" 
                                Name="chkToggleButton" 
                                Content="Toggle Button" 
                                igRibbon:RibbonGroup.MaximumSize="ImageAndTextLarge"  
                                Click="chkToggleButton_Checked">
                            </igRibbon:ToggleButtonTool>
                        </igRibbon:RibbonGroup>
                        <igRibbon:RibbonGroup Id="grpTestGroup2" Name="TestGroup2" Caption="Test Group 2">
                            <igRibbon:ButtonTool 
                                Id="btnSomeOtherButton" 
                                Name="btnSomeOtherButton" 
                                Content="Some Other Button" 
                                igRibbon:RibbonGroup.MaximumSize="ImageAndTextLarge">
                            </igRibbon:ButtonTool>
                        </igRibbon:RibbonGroup>
                    </igRibbon:RibbonTabItem>
                </igRibbon:XamRibbon>
            </igRibbon:RibbonWindowContentHost.Ribbon>
        </igRibbon:RibbonWindowContentHost>
    </igRibbon:XamRibbonWindow>

    VB code:

    Class frmTest_xamRibbonSaveButtonsFromQuickAccessBar
        Private DatasetIs_OK As Boolean = False
        Friend GUI_Items As New System.Data.DataSet
        Friend tblQuickAccessToolbarItems As System.Data.DataTable = GUI_Items.Tables.Add("tblQuickAccessToolbarItems")
        Friend strType As Type = System.Type.GetType("System.String")

        'We use it to restore buttons on QuickAccessBar and set IsChecked property for chkToggleButton
        Private Sub XamRibbonWindow_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
            If BuildDataset_ForItemsFromQuickAccessToolbar() Then
                LoadItemsOn_QuickAccessToolbar()
            End If
            chkToggleButton.IsChecked = My.Settings.chkToggleButtonIsChecked
        End Sub

        Private Sub chkToggleButton_Checked(sender As System.Object, e As System.Windows.RoutedEventArgs)
            My.Settings.chkToggleButtonIsChecked = chkToggleButton.IsChecked
        End Sub

        'We use it to save buttons from QuickAccessBar into application settings
        Private Sub XamRibbonWindow_Closing(sender As System.Object, e As System.ComponentModel.CancelEventArgs)
            If DatasetIs_OK Then
                SaveItemsFrom_QuickAccessToolbar()
            End If
            My.Settings.Save()
        End Sub

        Sub LoadItemsOn_QuickAccessToolbar()
            Try
                'Read data into dataset from app settings
                If Not My.Settings.chkToggleButtonIsChecked.ToString.Trim.Length = 0 Then
                    Dim stream_reader As New System.IO.StringReader(My.Settings.QuickAccessToolBarItems)
                    tblQuickAccessToolbarItems.ReadXml(stream_reader)
                End If

                If tblQuickAccessToolbarItems.Rows.Count > 0 Then
                    For i As Integer = 0 To tblQuickAccessToolbarItems.Rows.Count - 1

                        'We want only buttons that is why we check for 'grp', 
                        'in case user decided to add whole RibbonGroup to the QuickAccessToolbar
                        If Not tblQuickAccessToolbarItems.Rows(i)("TargetId").ToString.StartsWith("grp"Then

                            'Restore item on the QuickAccessToolbar
                            Dim newQuickAccessToolbarItem As New Infragistics.Windows.Ribbon.QatPlaceholderTool()
                            newQuickAccessToolbarItem.TargetId = tblQuickAccessToolbarItems.Rows(i)("TargetId").ToString
                            newQuickAccessToolbarItem.TargetType = Infragistics.Windows.Ribbon.QatPlaceholderToolType.Tool
                            xamRibbon.QuickAccessToolbar.Items.Add(newQuickAccessToolbarItem)
                        End If
                    Next
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Load Items On QuickAccessToolbar ERROR")
            End Try
        End Sub

        Sub SaveItemsFrom_QuickAccessToolbar()
            Try
                'We want to have current state of the QuickAccessToolbar, 
                'that is why - clean an old stuff first
                tblQuickAccessToolbarItems.Clear()

                Dim string_writer As New System.IO.StringWriter

                If xamRibbon.QuickAccessToolbar.Items.Count > 0 Then
                    For Each item As Object In xamRibbon.QuickAccessToolbar.Items
                        Dim tItem As Object = item.TargetType
                        Dim x As New System.Xml.Serialization.XmlSerializer(tItem.GetType)
                        string_writer = New System.IO.StringWriter
                        x.Serialize(string_writer, tItem)

                        'We are saving only buttons, that is why we check for 'grp',
                        'in case if user decided to add whole RibbonGroup to the QuickAccessToolbar
                        If Not item.TargetID.ToString.StartsWith("grp"Then
                            Dim nr As System.Data.DataRow = tblQuickAccessToolbarItems.NewRow
                            nr("TargetId") = item.TargetID
                            nr("TargetType") = string_writer.ToString
                            tblQuickAccessToolbarItems.Rows.Add(nr)
                        End If
                    Next
                End If
                'Write dataset into app settings
                string_writer = New System.IO.StringWriter
                tblQuickAccessToolbarItems.WriteXml(string_writer)
                My.Settings.QuickAccessToolBarItems = string_writer.ToString
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Save Items From QuickAccessToolbar ERROR")
            End Try
        End Sub

        Function BuildDataset_ForItemsFromQuickAccessToolbar() As Boolean
            If Not AddColumn(tblQuickAccessToolbarItems, "TargetId", strType, FalseFalseThen Return False
            If Not AddColumn(tblQuickAccessToolbarItems, "TargetType", strType, FalseFalseThen Return False
            DatasetIs_OK = True
            Return True
        End Function

        Function AddColumn(ByRef myTable As System.Data.DataTableByVal colName As StringByVal colType As TypeByVal AllowNulls As BooleanOptional ByVal AutoIncrement As Boolean = FalseAs Boolean
            Try
                Dim newCol As New System.Data.DataColumn(colName, colType)
                If AutoIncrement And AllowNulls Then
                    AutoIncrement = False
                End If
                newCol.AllowDBNull = AllowNulls
                newCol.AutoIncrement = AutoIncrement
                myTable.Columns.Add(newCol)
                Return True
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Add Column Into Table ERROR")
                Return False
            End Try
        End Function
    End Class
    Good luck.
  • 54937
    Suggested Answer
    Offline posted

    Currently there is no built in save/load functionality in the xamRibbon. You should submit a suggestion for adding this. In the interim you would need to save this manually. You could possibly iterate the Items collection of the QuickAccessToolbar and save out the TargetId of the QatPlaceholderTool instances in that collection and then use that to build new QatPlaceholderTool instances when the application is loaded.