0
votes

I have weird behaviors with binding to comboboxes, but when I search online, what I did should work... I think. I'm a nooby in WPF and binding stuff...I have only some basics understanding of this.

So basically I have 2 comboboxes to show COM ports and baudrate and I want to bind two thing to them, the Itemsource and the SelectedValue, because I want to show stuff and when I select something I want to have the selected string somewhere.

The weird behavior is different with both comboboxes...

With the combobox where I want to show the Baudrate, I generate a list of string in the constructor {9600, 19200, etc...}. The binding works with that when the application starts, I see the values in the combobox. For testing and learning purpose, I created a method that adds another value to the List. Normally the newly addeds string should also display in the combobox because it is bound and I implemented the INotifyPropertyChanged. What is happening is weird, when the application loads, the binding works, if I click on the combobox I see all the string that I added in the constructor. Then if I click on the button to add a string to the list and then click on the combobox again, the new value is not displaying. But at the startup of the application, if I do not touch the combobox and click on the button to add a string first then click on the combobox, it shows the newly added string. The thing is that from the moment I click on the combobox the newly added string do not display anymore.

And for the combox where I want to display the COM port names, nothing shows unless I use a List property directly in the viewmodel. If I expose a class containing the property I want to use, it just doesn't work. Like if instead of using PortName.DropdowList I use the property Names which is a directly a List in the viewmodel, when the method Refreshport runs it works, a COMX shows in the combobox, but not with my custom class.

In the end it is probably the same issue on both because I bind them the same way but use the properties inthe viewmodel differently...

Thanks for insights.

To see it in code:

The Model class for the comboboxes:

public class Combobox
{
    public Combobox()
    {
        
    }
    public Combobox(List<string> list)
    {
        DropdownList = list;
    }

    public List<string> DropdownList { get; set; }
    public string SelectedItemString { get; set; }
}

ViewModel Class:

    public class MainWindowViewModel : BaseViewModel
{
    SerialPort SerialPort = new SerialPort();
    
    public MainWindowViewModel()
    {
        this.RefreshPortCommand = new RefreshPortCommand(this);

        this.Baudrate = new Combobox(new List<string>() { "9600", "19200", "38400", "57600", "115200" });
        this.PortName = new Combobox();
        this.Rates = new List<string>() { "9600", "19200", "38400", "57600", "115200" };
        this.Names = new List<string>();
    }

    /// <summary>
    /// Command property declarations
    /// </summary>
    public RefreshPortCommand RefreshPortCommand { get; set; }
    public OpenPortCommand OpenPortCommand { get; set; }
    public THC4BootloaderCommand THC4BootloaderCommand { get; set; }
    #region Properties
    /// <summary>
    /// property declaration format with INotifyPropertyChanged
    /// </summary>
    private int myVar;
    public int MyProperty
    {
        get { return myVar; }
        set
        {
            myVar = value;
            RaisePropertyChanged("MyProperty");
        }
    }
    private List<string> _Names;

    public List<string> Names
    {
        get { return _Names; }
        set
        {
            _Names = value;
            RaisePropertyChanged("Names");
        }
    }

    private string _SelectedRate;

    public string SelectedRate
    {
        get { return _SelectedRate; }
        set
        {
            _SelectedRate = value;
            RaisePropertyChanged("SelectedRate");
        }
    }

    private List<string> _Rates;

    public List<string> Rates
    {
        get { return _Rates; }
        set
        {
            _Rates = value;
            RaisePropertyChanged("Rates");
        }
    }

    private Combobox _Baudrate;
    public Combobox Baudrate
    {
        get { return _Baudrate; }
        set
        {
            _Baudrate = value;
            RaisePropertyChanged("Baudrate");
        }
    }


    private Combobox _PortName;

    public Combobox PortName
    {
        get { return _PortName; }
        set
        {
            _PortName = value;
            RaisePropertyChanged("PortName");
        }
    }

    #endregion


    /// <summary>
    /// Method 
    /// </summary>

    public void RefreshPorts()
    {
        Baudrate.DropdownList.Add("12345");
        PortName.DropdownList = SerialPort.GetPortNames().ToList();
    }
}

The XAML:

<ComboBox Width="100"
          ItemsSource="{Binding PortName.DropdownList}"
          SelectedValue="{Binding PortName.SelectedItemString, Mode=OneWayToSource}"
          SelectedIndex="0"/>

<ComboBox Width="100" 
          ItemsSource="{Binding Baudrate.DropdownList}"
          SelectedValue="{Binding Baudrate.SelectedItemString, Mode=OneWayToSource}"
          SelectedIndex="2"/>
1

1 Answers

0
votes

You need to use ObservableCollection<T> instead of using List<T>.