简体   繁体   English

WPF 在 Combobox 中设置启动值

[英]WPF set startup value in Combobox

I have a WPF application where the user can set the serial port setting.我有一个 WPF 应用程序,用户可以在其中设置串行端口设置。 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.这些设置存储在 app.config 文件中。 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.对于奇偶校验,Stopbits 和 Comport 工作正常,但对于波特率和数据位数,我无法在启动时设置默认值。 The only difference for the baudrate and the databits is that i add the items for the combobox in the xaml file.波特率和数据位的唯一区别是我在 xaml 文件中添加了组合框的项目。 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 Parity、Stopbits 和 portname 的组合框通过

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 :在“Window_loaded”方法中,我这样做:

 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.这里的问题是我无法从 app.config 设置波特率和数据位值。 The other settings work fine.其他设置工作正常。

How can i solve this issue我该如何解决这个问题

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.那是因为 ConfigurationManager.AppSettings.Get("Baudrate") 和 ConfigurationManager.AppSettings.Get("Databits") 返回一个字符串,但 Baudrate & Databits 组合框上的 Items\\SelectedItem 是 ComboBoxItems。

Instead you could set SelectedValuePath="Content" on your Baudrate & Databits comboboxes set SelectedValue instead of SelectedItem:相反,您可以在 Baudrate & Databits 组合框上设置SelectedValuePath="Content"设置 SelectedValue 而不是 SelectedItem:

BaudrateCmbbox.波特率Cmbbox。 SelectedValue = ConfigurationManager.AppSettings.Get("Baudrate"); SelectedValue = ConfigurationManager.AppSettings.Get("波特率"); DatabitsCmbbox.数据位Cmbbox。 SelectedValue = ConfigurationManager.AppSettings.Get("Databits") SelectedValue = ConfigurationManager.AppSettings.Get("Databits")

Alternatively though, you could just set these two the same way as the others, from your config file.或者,您可以从配置文件中以与其他相同的方式设置这两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM