Search code examples
wpfvb.netxamlvml

How to initialise or bind ListItems that are defined in App.config into a VB.Net wpf ComboBox


I am designing a test-app that will be used in multiple stations in a flexible way so that the user can define the purpose of the use. Previously I have set this up using a txt .ini file (using VB 6 or Python) but following the suggestions of the VB.Net philosophy I am attempting to use the XML App.config file to work as the initialisation data-source. Following this approach I have been guided to use the machine-coding facilities of the App.config set-up table (by first entering a string into the value-field of the settings table, entering it and then erasing it). Thus I have a machine-written snippet of VML code which I attach below:

<?xml version="1.0" encoding="utf-8" ?>

In accordance with my intention to populate the Combo-box with a list of strings which can be selected by the user to propagate an option-specific configuration I have selected: System.Collections.Specialized.StringCollection as the variable type in the settings table for this app.

My difficulties are that I cannot find any definition of how to enter a list of strings into the App.config XML file. Furthermore I cannot see how to bind such a list of strings to a ListIndex in a WPF ComboBox. I can find examples of binding between controls on a page but none from the XML App.config file which I assume is handled by the Application Start-up event.

Any insight would be most welcome.


Solution

  • How about a completely different approach. Save your list as XML from a separate Utility . Then deserialize and bind to ComboBox.

    Class MainWindow
        Private lst As New List(Of String)
    Private Sub btnFillCombo_Click(sender As Object, e As RoutedEventArgs) Handles btnFillCombo.Click
        '1. Create an instance of XML Serializer
        Dim xs As XmlSerializer = New XmlSerializer(GetType(List(Of String)))
        '2. Create a Stream for your file
        Using fs As Stream = New FileStream("MyList.xml", FileMode.Open, FileAccess.Read, FileShare.None)
            '3. Deserialize your Stream and cast to List(Of String)
            lst = CType(xs.Deserialize(fs), List(Of String))
        End Using
        cboList.ItemsSource = lst
    End Sub
    
    
        Private Sub btnSaveList_Click(sender As Object, e As RoutedEventArgs) Handles btnSaveList.Click
            ' FileStream(FileName, Mode, Access, Share) intellisense overload #9 out of 15
            Using fs As Stream = New FileStream("MyList.xml", FileMode.Create, FileAccess.Write, FileShare.None)
                Dim xs As XmlSerializer = New XmlSerializer(GetType(List(Of String)))
                xs.Serialize(fs, lst)
            End Using
        End Sub
    
        Private Sub btnLoadList_Click(sender As Object, e As RoutedEventArgs) Handles btnLoadList.Click
            lst.Add("Mathew")
            lst.Add("Mark")
            lst.Add("Luke")
            lst.Add("John")
        End Sub
    End Class
    

    The btnLoadList and the btnSaveList would be in a separate Utility only run once.