0
votes

I have a WPF application where the user can set the serial port setting. At startup i want to set the comboboxes for the serialport settings with the last setting used previously. These settings are stored in the app.config file. For the Parity, Stopbits and Comport is works fine but for the baudrate and number of databits i'm not able to set the default value at startup. The only difference for the baudrate and the databits is that i add the items for the combobox in the xaml file. For the other settings i add the items dynamically at startup.

For the baudrate and i do :

<ComboBox x:Name="BaudrateCmbbox" FontSize="12" SelectionChanged="BaudrateCmbbox_SelectionChanged">
      <ComboBoxItem>9600</ComboBoxItem>
      <ComboBoxItem>19200</ComboBoxItem>
      <ComboBoxItem>38400</ComboBoxItem>
      <ComboBoxItem>115200</ComboBoxItem>
</ComboBox>

For the databits i do

<ComboBox x:Name="DatabitsCmbbox" FontSize="12" SelectionChanged="DatabitsCmbbox_SelectionChanged">
    <ComboBoxItem>7</ComboBoxItem>
    <ComboBoxItem>8</ComboBoxItem>
</ComboBox>

The comboboxes for Parity, Stopbits and portname are filled via

private  void SetPortParity()
    {
        foreach (string s in Enum.GetNames(typeof(Parity)))
        {
           ParityCmbbox.Items.Add(s);
        }
    }

    private void SetPortName()
    {
        foreach (string s in SerialPort.GetPortNames())
        {
            ComportsCmbbox.Items.Add(s);
        }
    }
    
    private void Stopbits()
    {
        foreach (string s in Enum.GetNames(typeof(StopBits)))
        {
            StopbitsCmbbox.Items.Add(s);
        }
    }

In the "Window_loaded" methode i do :

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        
        SetPortParity();
        SetPortName();
        Stopbits();
       

        BaudrateCmbbox.SelectedItem = ConfigurationManager.AppSettings.Get("Baudrate");
        DatabitsCmbbox.SelectedItem = ConfigurationManager.AppSettings.Get("Databits");
        ParityCmbbox.SelectedItem = ConfigurationManager.AppSettings.Get("Parity");
        StopbitsCmbbox.SelectedItem = ConfigurationManager.AppSettings.Get("StopBits");
        ComportsCmbbox.SelectedItem = ConfigurationManager.AppSettings.Get("Comport");

    }

The problem here is that i can't set the baudrate and databits values from the app.config. The other settings work fine.

How can i solve this issue

1

1 Answers

0
votes

That's because ConfigurationManager.AppSettings.Get("Baudrate") and ConfigurationManager.AppSettings.Get("Databits") returns a string but the Items\SelectedItem on your Baudrate & Databits comboboxes are ComboBoxItems.

Instead you could set SelectedValuePath="Content" on your Baudrate & Databits comboboxes set SelectedValue instead of SelectedItem:

BaudrateCmbbox.SelectedValue = ConfigurationManager.AppSettings.Get("Baudrate"); DatabitsCmbbox.SelectedValue = ConfigurationManager.AppSettings.Get("Databits")

Alternatively though, you could just set these two the same way as the others, from your config file.